二叉树最大深度:一行递归代码,O(n) 时间

9 阅读1分钟

[> ******](## 二叉树最大深度

var maxDepth = function(root) {
    if (!root) return 0;
    return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
};

核心公式:深度 = 1 + Math.max(左子树深度, 右子树深度)。)

cover.png