[437]. Path Sum III,[687]. Longest Univalue Path
2017-11-21 02:54
295 查看
[437]. Path Sum III
You are given a binary tree in which each node contains an integer value.
Find the number of paths that sum to a given value.
The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).
The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.
我把这两题放在一起就是因为这两题有共同的问题,容易在递归的时候重复计算一些情况。当递归中出现分叉的时候,一定要确保各司其职,不要重复。
这题就是,在每个节点都有两个选项,要么就是包含这个节点的值继续往下走,要么就是不要这个节点的值从下个节点开始。带着节点值往下走的就不需要考虑两种情况了,否则就重复了。所以代码如下:
[687]. Longest Univalue Path
Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root.
Note: The length of path between two nodes is represented by the number of edges between them.
这题也是一样,对每个节点来说,包含两种情况,要么不要这个节点,那么从这个节点开始的树中,最长的路径就是左子树和右子树里最长的路径。要么包含这个节点,那么最长的路径就是左边连着这个点的自上而下的最长路径,加上右边连着这个点的自上而下的最长路径。
You are given a binary tree in which each node contains an integer value.
Find the number of paths that sum to a given value.
The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).
The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.
Example: root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8 10 / \ 5 -3 / \ \ 3 2 11 / \ \ 3 -2 1 Return 3. The paths that sum to 8 are: 1. 5 -> 3 2. 5 -> 2 -> 1 3. -3 -> 11
我把这两题放在一起就是因为这两题有共同的问题,容易在递归的时候重复计算一些情况。当递归中出现分叉的时候,一定要确保各司其职,不要重复。
这题就是,在每个节点都有两个选项,要么就是包含这个节点的值继续往下走,要么就是不要这个节点的值从下个节点开始。带着节点值往下走的就不需要考虑两种情况了,否则就重复了。所以代码如下:
class Solution { public: int pathSum(TreeNode* root, int sum) { if (root == NULL) return 0; int cnt = 0; if (root->val == sum) cnt++; cnt += cumulate_helper(root->left, sum, root->val); cnt += cumulate_helper(root->right, sum, root->val); cnt += pathSum(root->left, sum); cnt += pathSum(root->right, sum); return cnt; } int cumulate_helper(TreeNode* root, int sum, int tempsum) { if (root == NULL) return 0; tempsum += root->val; int cnt = 0; if (tempsum == sum) cnt++; cnt += cumulate_helper(root->left, sum, tempsum); cnt += cumulate_helper(root->right, sum, tempsum); return cnt; } };
[687]. Longest Univalue Path
Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root.
Note: The length of path between two nodes is represented by the number of edges between them.
Example 1: Input: 5 / \ 4 5 / \ \ 1 1 5 Output: 2
这题也是一样,对每个节点来说,包含两种情况,要么不要这个节点,那么从这个节点开始的树中,最长的路径就是左子树和右子树里最长的路径。要么包含这个节点,那么最长的路径就是左边连着这个点的自上而下的最长路径,加上右边连着这个点的自上而下的最长路径。
class Solution { public: int longestUnivaluePath(TreeNode* root) { if (root == NULL) return 0; int max1 = max(longestUnivaluePath(root->left), longestUnivaluePath(root->right)); int max2 = 0; if (root->left && root->val == root->left->val) max2 += helper(root->left); if (root->right && root->val == root->right->val) max2 += helper(root->right); return max(max1, max2); } int helper(TreeNode* root) { int cntl = 1; int cntr = 1; if (root->left && root->left->val == root->val) cntl += helper(root->left); if (root->right && root->right->val == root->val) cntr += helper(root->right); return max(cntl, cntr); } };
相关文章推荐
- leetcode[437] PathSum III 个人题解
- 437_Path_Sum_III
- 437. Path Sum III
- LeetCode 437 Path Sum III
- Leetcode算法学习日志-437 Path Sum III
- Leetcode算法学习日志-687 Longest Univalue Path
- 437. Path Sum III
- 437. Path Sum III
- LeetCode 437 Path Sum III
- leetcode(437):Path Sum III
- 437. Path Sum III
- 437. Path Sum III
- LeetCode 437 Path Sum III (DFS)
- 437. Path Sum III
- LeetCode.112(113/437) Path Sum I && II && III
- 437. Path Sum III
- 437. Path Sum III
- 437. Path Sum III
- [python]leetcode(437). Path Sum III
- 437. Path Sum III