二叉树的路径和

来源:互联网 发布:java重载与多态 编辑:程序博客网 时间:2024/05/19 12:35
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    }};


0 0