码迷,mamicode.com
首页 > 其他好文 > 详细

Path Sum II

时间:2015-01-25 01:26:09      阅读:175      评论: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> >ans;
    vector<vector<int> > pathSum(TreeNode *root, int sum) {
        vector<int>v;
        Sum(root,sum,v);
        return ans;
    }
    void Sum(TreeNode *root, int sum, vector<int>res){
      if(root == NULL) return ;
      res.push_back(root->val);
      if(root->val == sum && root->left == NULL && root->right == NULL)
        ans.push_back(res);
       Sum(root->left,sum-root->val,res);
       Sum(root->right,sum-root->val,res);
    }
};


Path Sum II

标签:

原文地址:http://blog.csdn.net/sina012345/article/details/43103105

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!