-
Notifications
You must be signed in to change notification settings - Fork 70
/
res.js
38 lines (33 loc) · 773 Bytes
/
res.js
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
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var longestUnivaluePath = function(root) {
if (!root) return 0;
let compRes = 0;
const getMaxPath = (ele) => {
if (ele == null) return 0;
const val = ele.val;
let left = 0;
let right = 0;
let tl = getMaxPath(ele.left);
let tr = getMaxPath(ele.right);
if (ele.left !== null && ele.left.val === val) {
left = tl + 1;
}
if (ele.right !== null && ele.right.val === val) {
right = tr + 1;
}
compRes = Math.max(compRes, left + right);
return Math.max(left, right);
}
getMaxPath(root);
return compRes;
};