class Solution {
public:
int ans=0;
int getlength(TreeNode* root){
if(!root)return 0;
int left=getlength(root->left);
int right=getlength(root->right);
if(root->left!=nullptr&&root->left->val==root->val)++left;
else left=0;
if(root->right!=nullptr&&root->right->val==root->val) ++right;
else right=0;
ans=max(ans,left+right);
return max(left,right);
}
int longestUnivaluePath(TreeNode* root) {
getlength(root);
return ans;
}
};