687. Longest Univalue Path
2017-11-13 20:10
232 查看
Problem:
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.
Solution:
class Solution {
public:
int longestUnivaluePath(TreeNode* root) {
if (root == NULL)
return 0;
return max(longestUnivaluePath(root->left) + 1, longestUnivaluePath(root->right) + 1);
}
};
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.
Solution:
class Solution {
public:
int longestUnivaluePath(TreeNode* root) {
if (root == NULL)
return 0;
return max(longestUnivaluePath(root->left) + 1, longestUnivaluePath(root->right) + 1);
}
};
相关文章推荐
- 687. Longest Univalue Path
- leetcode 687. Longest Univalue Path
- leetcode 687. Longest Univalue Path 二叉树最长相等路径+ 深度优先遍历DFS
- 687. Longest Univalue Path
- LeetCode 687. Longest Univalue Path(java)
- LWC 52:687. Longest Univalue Path
- (树)687. Longest Univalue Path
- [leetcode] 687. Longest Univalue Path
- [Leetcode] 687. Longest Univalue Path 解题报告
- 687. Longest Univalue Path
- 【leetcode】687. Longest Univalue Path解题报告
- leetcode 687. Longest Univalue Path
- 687. Longest Univalue Path
- 687. Longest Univalue Path
- LeetCode687. Longest Univalue Path
- 687. Longest Univalue Path
- Leetcode刷题:687. Longest Univalue Path
- [LeetCode] 687. Longest Univalue Path 最长唯一值路径
- leetcode_687. Longest Univalue Path ? 待解决
- [LeetCode - tree]687. Longest Univalue Path