-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path687.cpp
29 lines (29 loc) · 896 Bytes
/
687.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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
// Runtime: 120 ms, faster than 95.67% of C++ online submissions for Longest
// Univalue Path. Memory Usage: 49.7 MB, less than 64.70% of C++ online
// submissions for Longest Univalue Path.
public:
int longestUnivaluePath(TreeNode *root) {
int maxLen = 0;
function<int(TreeNode *, TreeNode *)> dfs = [&](TreeNode *root,
TreeNode *parent) {
if (!root)
return 0;
int l = dfs(root->left, root);
int r = dfs(root->right, root);
maxLen = max(maxLen, l + r);
return not parent or parent->val == root->val ? max(l, r) + 1 : 0;
};
dfs(root, nullptr);
return maxLen;
}
};