jzhangnu / Leetcode-JS-Solutions

:tropical_drink: Leetcode solutions using JavaScript.
52 stars 7 forks source link

513. Find Bottom Left Tree Value #145

Open jzhangnu opened 6 years ago

jzhangnu commented 6 years ago
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;
};