class Solution {
public:
/**
* @param root the root of binary tree
* @param target an integer
* @return all valid paths
*/
vector<vector<int>> a;
vector<int> b;
int sum;
int sum1;
void look(TreeNode *root)
{
if(root==NULL)
return ;
sum+=root->val;
b.push_back(root->val);
if(sum==sum1&&root->left==NULL&&root->right==NULL)
{
a.push_back(b);
b.pop_back();
sum-=root->val;
return;
}
look(root->left);
look(root->right);
sum-=root->val;
b.pop_back();
}
vector<vector<int>> binaryTreePathSum(TreeNode *root, int target)
{
sum=0;
sum1=target;
look(root);
return a;
// Write your code here
}
};
二叉树的路径和 - C++
最新推荐文章于 2022-02-22 15:30:41 发布