Leetcode-113 Path Sum II(路径总和 II)

 1 class Solution
 2 {
 3     public:
 4         vector<vector<int>> res;
 5         int SUM;
 6         void dfs(TreeNode* node,int tt,vector<int> tmp)
 7         {
 8             vector<int> tmp1 = tmp,tmp2 = tmp;
 9             cout << tmp.size() << endl;
10             if(node->left)
11             {
12                 tmp1.push_back(node->left->val);
13                 dfs(node->left,tt+node->left->val,tmp1);
14             }
15             if(node->right)
16             {
17                 tmp2.push_back(node->right->val);
18                 dfs(node->right,tt+node->right->val,tmp2);
19             }
20             if(!node->left && !node->right && tt==SUM)
21             {
22                 res.push_back(tmp);
23             }
24         }
25         vector<vector<int>> pathSum(TreeNode* root, int sum)
26         {
27             if(!root)
28                 return res;
29             vector<int> tmp;
30             tmp.push_back(root->val);
31             SUM = sum;
32             int tt = root->val;
33             dfs(root,tt,tmp);
34             return res;
35         }
36 };

 

posted @ 2018-11-25 20:23  Asurudo  阅读(148)  评论(0编辑  收藏  举报