Javascript, 98ms, beats 100.0%


  • 0
    S

    Somewhat surprised such a simple solution came out on top. This solution does a preorder traversal and keeps track of the depth of the current result. If the depth is surpassed, then the leftmost node on the next layer has been found.

    var findBottomLeftValue = function(root) {
        var result = root.val;
        var resultHeight = 0;
        (function recurse (node, height) {
            if (node === null) {
                return;
            }
            if (node.left !== null) {
                recurse(node.left, height + 1);
            }
            if (height > resultHeight) {
                result = node.val;
                resultHeight = height;
            }
            if (node.right !== null) {
                recurse(node.right, height + 1);
            }
        })(root, 1);
        return result;
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.