Lowest Common Ancestor of Deepest Leaves LeetCode solution in JavaScript

1
var lcaDeepestLeaves = function (root) {
2
if (!root) return root;
3
// keep track of max depth if node have both deepest node
4
let md = 0,
5
ans = null;
6
const compute = (r = root, d = 0) => {
7
if (!r) {
8
md = Math.max(md, d);
9
return d;
10
}
11

12
const ld = compute(r.left, d + 1);
13
const rd = compute(r.right, d + 1);
14

15
if (ld == rd && ld == md) {
16
ans = r;
17
}
18

19
return Math.max(ld, rd);
20
};
21
compute();
22
return ans;
23
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0