首页 > 其他 > 详细

Path Sum II

时间:2015-03-16 23:01:47      阅读:393      评论:0      收藏:0      [点我收藏+]
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int> >res;//记录结果
    void dfs(TreeNode *root,int sum,vector<int> temp){//temp记录每一条可能的路径
        if(!root) return;
        if(!root->left&&!root->right&&sum == root->val){//满足条件
            temp.push_back(sum);
            res.push_back(temp);
        }
        temp.push_back(root->val);
        dfs(root->left,sum-root->val,temp);
        dfs(root->right,sum-root->val,temp);
    }
    vector<vector<int> > pathSum(TreeNode *root, int sum) {
        vector<int>temp;
        dfs(root,sum,temp);
        return res;
    }
};

 

Path Sum II

原文:http://www.cnblogs.com/llei1573/p/4342965.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!