LeetCode_687
作者:互联网
class Solution {
public:
int longestUnivaluePath(TreeNode* root) {
if(root == nullptr) return 0;
int ans = 0;
univalue(root, &ans);
return ans;
}
private:
int univalue(TreeNode& root, int* ans) {
if(root == nullptr) return 0;
int l = univalue(root->left, ans);
int r = univalue(root->right, ans);
int pl = 0;
int pr = 0;
if(root->left && root.val == root->left.val) pl = l + 1;
if(root->left && root.val == root->right.val) pr = r + 1;
*ans = max(*ans, pr + pl);
return max(pl,pr);
}
};
标签:pr,return,val,int,ans,687,root,LeetCode 来源: https://www.cnblogs.com/huangming-zzz/p/11243333.html