[leetCode]Path Sum&&Path Sum II
2014-03-09 17:18
351 查看
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->2which sum is 22.
Path Sum很好做,只要递归时sum为0则返回true就好了。
#include <iostream> #include <vector> #include <stack> using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; class Solution { public: bool hasPathSum(TreeNode *root, int sum) { if(root == NULL) return false; sum = sum - root->val; bool isThisPath = false; if(root->left != NULL) isThisPath = hasPathSum(root->left,sum); if(isThisPath) return true; if(root->right != NULL) isThisPath = hasPathSum(root->right,sum); if(isThisPath) return true; if(root->left == NULL && root->right == NULL && sum == 0) return true; if(root->left == NULL && root->right == NULL && sum != 0) return false; return isThisPath; } }
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 SumII 做的稍微久一点,一开始递归没想清楚。
Path SumII是找从根节点到叶结点的节点中有多少条分支的和可以为一个确定值的问题,输出是路径的集合。
用递归来思考的话还是很简单的,每个节点只要确定他的孩子节点是不是在路径上(即孩子节点是不是在和为定值sum的路径上),如果是,则把自己加入,如果不是,则不加入。
递归的结束条件则是 如果节点为叶子节点,且sum在此时为0.
这个基础上,两种解法:
解法一是自顶向下,如果父节点发现本身是在和为定值的路径上(使用Path SumI的结果),则将自己加入路径节点中,然后遍历自己的子节点,依次循环,最后在叶结点中将整条路径加入路径的集合中。
解法一代码:
/** * 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> > retvv; bool hasPathSum(TreeNode *root, int sum) { if(root == NULL) return false; sum = sum - root->val; bool isThisPath = false; if(root->left != NULL) isThisPath = hasPathSum(root->left,sum); if(isThisPath) return true; if(root->right != NULL) isThisPath = hasPathSum(root->right,sum); if(isThisPath) return true; if(root->left == NULL && root->right == NULL && sum == 0) return true; if(root->left == NULL && root->right == NULL && sum != 0) return false; return isThisPath; } vector<vector<int> > pathSum(TreeNode *root, int sum){ vector<int> path; if(root== NULL) return retvv; return onePath(root,sum,path); } vector<vector<int> > onePath(TreeNode *root, int sum, vector<int> path){ if(root == NULL) path.clear(); int cursum = sum - root->val; if(root->left == NULL && root->right == NULL && cursum == 0){ path.push_back(root->val); retvv.push_back(path); return retvv; } if(root->left == NULL && root->right == NULL && cursum != 0){ return retvv; } if(hasPathSum(root,sum)) { path.push_back(root->val); if(hasPathSum(root->left,cursum)) onePath(root->left,cursum,path); if(hasPathSum(root->right,cursum)) onePath(root->right,cursum,path); } return retvv; } };
解法二是自下向上,每次先确定其孩子节点在不在路径中,如果孩子节点在,则把自己加入路径中。
解法二代码:
#include <iostream> #include <vector> #include <stack> using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; class Solution { public: vector<vector<int> > retvv; vector<vector<int> > pathSum(TreeNode *root, int sum) { if(root == NULL) return retvv; sum = sum - root->val; if(root->left == NULL && root->right == NULL && sum == 0){ vector<int> vec; vec.push_back(root->val); retvv.push_back(vec); return retvv; } if(root->left == NULL && root->right == NULL && sum != 0){ return retvv; } size_t preSize = retvv.size(); if(root->left != NULL) pathSum(root->left,sum); if(root->right != NULL) pathSum(root->right,sum); for(preSize; preSize < retvv.size();preSize++){ retvv[preSize].insert(retvv[preSize].begin(),root->val); } return retvv; } };
实测解法一代码虽然长一些,但是运行速度比解法二快两倍多(320/840)。猜测应该是在vector中使用insert函数导致的。
相关文章推荐
- LeetCode-Path Sum II<ERROR>
- [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
- LeetCode之“树”:Path Sum && Path Sum II
- [LeetCode 112 113] - 路径和I & II (Path Sum I & II)
- leetcode -day17 Path Sum I II & Flatten Binary Tree to Linked List & Minimum Depth of Binary Tree
- [Leetcode]Path Sum&Path Sum II
- [Leetcode]Path Sum && Path Sum II
- Leetcode: Unique Path I & II, Minimum Path Sum, Triangle
- [LeetCode]112. Path Sum&113. Path Sum II
- 【LeetCode】Unique Paths I && II && Minimum Path Sum
- 【leetcode】Path Sum I && II
- Leetcode::Pathsum & Pathsum II
- [LeetCode] Path Sum && Path Sum II
- LeetCode: Unique Paths I & II & Minimum Path Sum
- [Leetcode][JAVA] Path Sum I && II
- Path Sum & Path Sum II | LeetCode DFS
- [leetcode]_Path Sum I && II
- [Leetcode] #112#113 Path Sum I & II
- 【leetcode】Path Sum I & II(middle)
- LeetCode:Path Sum I &&II