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

Path Sum系列

时间:2014-08-26 17:05:36      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   使用   io   for   ar   

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         if( !root ) return false;   //如果开始root就为空,说明就不存在
14         if( !root->left && !root->right ) return sum == root->val;  //如果是叶节点,那么直接判断sum是否等于叶节点的值
15         if( root->left && hasPathSum(root->left, sum-root->val) ) return true;  //需要确定左子树存在,才继续下搜
16         if( root->right && hasPathSum(root->right, sum-root->val) ) return true;    //同上
17         return false;
18     }
19 };

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]
]

 

 

同path sum II,依然使用先序遍历,不过加上path变量来记录路径,代码如下:
 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         allpath.clear();
14         if( !root ) return allpath;
15         vector<int> path;
16         preorder(path, root, sum);
17         return allpath;
18     }
19     
20     void preorder(vector<int>& path, TreeNode* root, int sum) {
21         if( !root->left && !root->right ) { //如果是叶节点
22             if( sum == root->val ) {    //如果总和刚好是sum,那么放入allpath中
23                 path.push_back(sum);
24                 allpath.push_back(path);
25                 path.pop_back();
26             }
27             return ;
28         }
29         if( root->left ) {  //确保是左子树存在,函数并没有处理空的情况
30             path.push_back(root->val);
31             preorder(path, root->left, sum-root->val);
32             path.pop_back();
33         }
34         if( root->right ) {
35             path.push_back(root->val);
36             preorder(path, root->right, sum-root->val);
37             path.pop_back();
38         }
39     }
40     
41 private:
42     vector< vector<int> > allpath;
43 };

 

 

Path Sum系列

标签:style   blog   http   color   os   使用   io   for   ar   

原文地址:http://www.cnblogs.com/bugfly/p/3937543.html

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