LeetCode:Path Sum I II

LeetCode:Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

For example:
Given the below binary tree and sum = 22,

              5
             / 
            4   8
           /   / 
          11  13  4
         /        
        7    2      1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

递归求解,代码如下:

 1 /**
 2  * Definition for binary tree
 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     bool hasPathSum(TreeNode *root, int sum) {
13         // IMPORTANT: Please reset any member data you declared, as
14         // the same Solution instance will be reused for each test case.
15         if(root == NULL)return false;
16         if(root->left == NULL && root->right == NULL && root->val == sum)
17             return true;
18         if(root->left && hasPathSum(root->left, sum - root->val))
19             return true;
20         if(root->right && hasPathSum(root->right, sum - root->val))
21             return true;
22         return false;
23     }
24 };

LeetCode:Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.

For example:
Given the below binary tree and sum = 22,

              5
             / 
            4   8
           /   / 
          11  13  4
         /      / 
        7    2  5   1

return

[
   [5,4,11,2],
   [5,8,4,5]
]                                                                       本文地址

同理也是递归求解,只是要保存当前的结果,并且每次递归出来后要恢复递归前的结果,每当递归到叶子节点时就把当前结果保存下来。

 1 /**
 2  * Definition for binary tree
 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> > pathSum(TreeNode *root, int sum) {
13         // IMPORTANT: Please reset any member data you declared, as
14         // the same Solution instance will be reused for each test case.
15         vector<vector<int> > res;
16         if(root == NULL)return res;
17         vector<int> curres;
18         curres.push_back(root->val);
19         pathSumRecur(root, sum, curres, res);
20         return res;
21     }
22     void pathSumRecur(TreeNode *root, int sum, vector<int> &curres, vector<vector<int> >&res)
23     {
24         if(root->left == NULL && root->right == NULL && root->val == sum)
25         {
26             res.push_back(curres);
27             return;
28         }
29         if(root->left)
30         {
31             curres.push_back(root->left->val);
32             pathSumRecur(root->left, sum - root->val, curres, res);
33             curres.pop_back();
34         }
35         if(root->right)
36         {
37             curres.push_back(root->right->val);
38             pathSumRecur(root->right, sum - root->val, curres, res);
39             curres.pop_back();
40         }
41     }
42 };

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3440044.html

原文地址:https://www.cnblogs.com/TenosDoIt/p/3440044.html