leetcode 113. Path Sum II

题目描述:

//深搜  结束条件 是叶子节点 并且当前路径的加和为sum 加入result中  否则退出。

/**
 * 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<vector<int> > ret;
    vector<vector<int>> pathSum(TreeNode* root, int sum) {
        if(root == NULL)
            return ret;
        vector<int> tmp;
        record(root,tmp,0,sum);
        return ret;
        
    }
    
    void record(TreeNode* root, vector<int> tmp , int now, int target){
        now += root->val;
        tmp.push_back(root->val);
        if(root->left == NULL && root->right == NULL){
            if(now == target)
                ret.push_back(tmp);
            return;
        }
        if(root->left != NULL)
            record(root->left,tmp,now,target);
        if(root->right != NULL)
            record(root->right,tmp,now,target);

    }
};
原文地址:https://www.cnblogs.com/strongYaYa/p/6772665.html