【leetcode】104. 二叉树的最大深度

35 阅读1分钟

leetcode-104.png

  • 如果当前节点为空(即为 null),那么树的深度为 0。
  • 如果当前节点不为空,递归地计算其左子树和右子树的最大深度,然后取两者的最大值,再加上当前节点的深度(即 1)。
var maxDepth = function (root) {
    if (!root) return 0
    return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1
};