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);
}
};