【easy】257. Binary Tree Paths 二叉樹找到所有路徑
阿新 • • 發佈:2018-02-13
result .net blog struct 所有 二叉 -s col res
http://blog.csdn.net/crazy1235/article/details/51474128
花樣做二叉樹的題……居然還是不會麽……
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void binaryTreePaths(vector<string>& result,TreeNode* root,string t)
{
if(!root->left&&!root->right)
{
result.push_back(t);
return;
}
if(root->left)
binaryTreePaths(result,root->left,t+" ->"+to_string(root->left->val));
if(root->right)
binaryTreePaths(result,root->right,t+"->"+to_string(root->right->val));
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> result;
if(root==NULL) return result;
binaryTreePaths(result,root,to_string(root->val));
return result;
}
};
【easy】257. Binary Tree Paths 二叉樹找到所有路徑