104. Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

 思路:根节点的最大深度 = 左右子节点中最大深度+1

var maxDepth = function(root) {
    if(root === null) return 0;
    
    var ld = maxDepth(root.left);
    var rd = maxDepth(root.right);
    return ld > rd ? ++ld : ++rd;  //写法等同于return Math.max(ld,rd)+1;
    
};
posted @ 2016-11-09 19:59  bubbleStar  阅读(130)  评论(0编辑  收藏  举报