二叉树中和为某一值的路径
class Solution {
public:
    vector<vector<int>> res;
    vector<int> path;
    void dfs(TreeNode* root, int sum,int t)
    {
        t+=root->val;
        path.push_back(root->val);
        if(root->left)
            dfs(root->left,sum,t);
        if(root->right)
            dfs(root->right,sum,t);
        if(!root->left&&!root->right&&sum==t)   res.push_back(path);
        t-=root->val;
        path.pop_back();
    }
    vector<vector<int>> findPath(TreeNode* root, int sum) {
        if(!root)   return res;
        dfs(root,sum,0);
        return res;
    }
};
    有帮助的话可以点个赞,我会很开心的~
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号