[leetcode]Binary Tree Paths
4857 ワード
注意事項
に答える
コード#コード#
C++
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
private:
vector<string> res;
public:
void TreePaths(TreeNode* root, string &paths){
if(root==NULL)
return;
string temp = paths;
if(root->left==NULL&&root->right==NULL){
paths += to_string(root->val);
res.push_back(paths);
paths = temp;
return;
}
paths += to_string(root->val)+"->";
TreePaths(root->left, paths);
TreePaths(root->right, paths);
paths = temp;
}
vector<string> binaryTreePaths(TreeNode* root) {
string paths = "";
TreePaths(root,paths);
return res;
}
};
Reference
この問題について([leetcode]Binary Tree Paths), 我々は、より多くの情報をここで見つけました https://velog.io/@6047198844/leetcodeBinary-Tree-Pathsテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol