Lowest Common Ancestor of Deepest Leaves LeetCode solution in JavaScript
1var lcaDeepestLeaves = function (root) {2if (!root) return root;3// keep track of max depth if node have both deepest node4let md = 0,5ans = null;6const compute = (r = root, d = 0) => {7if (!r) {8md = Math.max(md, d);9return d;10}1112const ld = compute(r.left, d + 1);13const rd = compute(r.right, d + 1);1415if (ld == rd && ld == md) {16ans = r;17}1819return Math.max(ld, rd);20};21compute();22return ans;23};