1
var subtreeWithAllDeepest = function (root) {
2
// keep track of deepest till now
3
// if left right has both deepest than it will be the ans;
4
let mx = 0,
5
rm = 0,
6
ans = null;
7
const compute = (r = root, d = 0) => {
8
if (!r) {
9
rm = Math.max(rm, d);
10
return d;
11
}
12
const ld = compute(r.left, d + 1);
13
const rd = compute(r.right, d + 1);
14

15
if (ld == rd && ld == mx) {
16
ans = r;
17
}
18
if (mx < rm) {
19
ans = r;
20
mx = rm;
21
}
22

23
return Math.max(ld, rd);
24
};
25
compute();
26
return ans;
27
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0