One line JS solution


  • 1
    R
    return (root === null ?  0 : 1 + Math.max(maxDepth(root.left),maxDepth(root.right)));

  • 0
    S
    var maxDepth = function(root) {
        if(!root) return 0;
        var l = maxDepth(root.left);
        var r = maxDepth(root.right);
        //var max = (l ^ ((l ^ r) & -(l < r))) + 1; // max(x, y)  // ~172ms
        //var max Math.max(l, r) + 1;                             // ~184ms
        return (l > r ? l : r) + 1;                               // ~220ms
    };

Log in to reply
 

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