您的位置:首页 > 产品设计 > UI/UE

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

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: