AcWing 47. 二叉树中和为某一值的路径
原题链接
中等
作者:
术
,
2021-04-27 17:02:12
,
所有人可见
,
阅读 231
/**
* 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:
vector<int> path;
vector<vector<int>> res;
vector<vector<int>> findPath(TreeNode* root, int sum) {
if(root==NULL) return res;
path.push_back(root->val);
//为叶子节点
if(root->left==NULL&&root->right==NULL)
if(sum==root->val)
//返回
res.push_back(path);
if(root->left!=NULL)
findPath(root->left,sum-root->val);
if(root->right!=NULL)
findPath(root->right,sum-root->val);
path.pop_back();
return res;
}
};