-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0549-binary-tree-longest-consecutive-sequence-ii.js
More file actions
61 lines (55 loc) · 1.73 KB
/
0549-binary-tree-longest-consecutive-sequence-ii.js
File metadata and controls
61 lines (55 loc) · 1.73 KB
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
55
56
57
58
59
60
61
/**
* 549. Binary Tree Longest Consecutive Sequence II
* https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/
* Difficulty: Medium
*
* Given the root of a binary tree, return the length of the longest consecutive path in the tree.
*
* A consecutive path is a path where the values of the consecutive nodes in the path differ by one.
* This path can be either increasing or decreasing.
* - For example, [1,2,3,4] and [4,3,2,1] are both considered valid, but the path [1,2,4,3] is
* not valid.
*
* On the other hand, the path can be in the child-Parent-child order, where not necessarily be
* parent-child order.
*/
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var longestConsecutive = function(root) {
let maxLength = 0;
traverse(root);
return maxLength;
function traverse(node) {
if (!node) return [0, 0];
let inc = 1;
let dec = 1;
if (node.left) {
const [leftInc, leftDec] = traverse(node.left);
if (node.val === node.left.val + 1) {
dec = Math.max(dec, leftDec + 1);
} else if (node.val === node.left.val - 1) {
inc = Math.max(inc, leftInc + 1);
}
}
if (node.right) {
const [rightInc, rightDec] = traverse(node.right);
if (node.val === node.right.val + 1) {
dec = Math.max(dec, rightDec + 1);
} else if (node.val === node.right.val - 1) {
inc = Math.max(inc, rightInc + 1);
}
}
maxLength = Math.max(maxLength, inc + dec - 1);
return [inc, dec];
}
};