LeetCode 113. Path Sum II

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<vector<int>> res;
13     
14     void help(TreeNode* root, int sum,vector<int> path){
15         if(!root) return;
16         if(!root->left && !root->right){
17             if(sum == root->val){
18                 path.push_back(root->val);
19                 res.push_back(path);
20             }
21             return;
22         }
23         path.push_back(root->val);
24         help(root->left, sum - root->val, path);
25         help(root->right, sum - root->val, path);
26     }
27     
28     vector<vector<int>> pathSum(TreeNode* root, int sum) {
29         
30         if(!root) return res;
31         vector<int> path;
32         help(root, sum, path);
33         return res;
34     }
35     
36     
37 };

 

posted @ 2016-03-05 10:38  co0oder  阅读(127)  评论(0编辑  收藏  举报