1var subtreeWithAllDeepest = function (root) {2// keep track of deepest till now3// if left right has both deepest than it will be the ans;4let mx = 0,5rm = 0,6ans = null;7const compute = (r = root, d = 0) => {8if (!r) {9rm = Math.max(rm, d);10return d;11}12const ld = compute(r.left, d + 1);13const rd = compute(r.right, d + 1);1415if (ld == rd && ld == mx) {16ans = r;17}18if (mx < rm) {19ans = r;20mx = rm;21}2223return Math.max(ld, rd);24};25compute();26return ans;27};