-
Notifications
You must be signed in to change notification settings - Fork 28
/
Longest_Univalue_Path.cpp
54 lines (48 loc) · 1.59 KB
/
Longest_Univalue_Path.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
// with single DFS(like diameter of a tree)
class Solution {
int longestUnivaluePath(TreeNode* root, int& longestPath) {
if(!root) return 0;
int leftDepth = longestUnivaluePath(root->left, longestPath);
int rightDepth = longestUnivaluePath(root->right, longestPath);
if(root->left and root->left->val == root->val) {
leftDepth++;
} else {
leftDepth = 0;
}
if(root->right and root->right->val == root->val) {
rightDepth++;
} else {
rightDepth = 0;
}
longestPath = max(longestPath, leftDepth + rightDepth);
return max(leftDepth, rightDepth);
}
public:
int longestUnivaluePath(TreeNode* root) {
int longestPath = 0;
longestUnivaluePath(root, longestPath);
return longestPath;
}
};
// O(n^2)
class Solution {
int longestUnivaluePath(TreeNode* root, int val) {
if(!root or root->val != val) return 0;
return 1 + max(longestUnivaluePath(root->left, val), longestUnivaluePath(root->right, val));
}
public:
int longestUnivaluePath(TreeNode* root) {
if(!root) return 0;
int sub = max(longestUnivaluePath(root->left), longestUnivaluePath(root->right));
return max(sub, longestUnivaluePath(root->left, root->val) + longestUnivaluePath(root->right, root->val));
}
};