-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathLongest ZigZag Path in a Binary Tree.js
41 lines (35 loc) · 1.24 KB
/
Longest ZigZag Path in a Binary Tree.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
39
40
41
/** https://leetcode.com/problems/longest-zigzag-path-in-a-binary-tree/
* 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 longestZigZag = function(root) {
this.out = 0;
// Recursive left and right node and find the largest height
let left = dfs(root.left, false) + 1;
let right = dfs(root.right, true) + 1;
this.out = Math.max(this.out, Math.max(left, right));
return this.out;
};
var dfs = function(node, isLeft) {
// Node is null, we return -1 because the caller will add 1, so result in 0 (no node visited)
if (node == null) {
return -1;
}
// No left or right node, we return 0 because the caller will add 1, so result in 1 (visited 1 node - this one)
if (node.left == null && node.right == null) {
return 0;
}
// Recursive to see which one is higher, zigzag to left or zigzag to right
let left = dfs(node.left, false) + 1;
let right = dfs(node.right, true) + 1;
this.out = Math.max(this.out, Math.max(left, right));
return isLeft === true ? left : right;
};