深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个结点只能访问一次。要特别注意的是,二叉树的深度优先遍历比较特殊,可以细分为先序遍历、中序遍历、后序遍历。

二叉树最大深度:

如果二叉树为空,则深度为0 
如果不为空,分别求左子树的深度和右子树的深度,取最大的再加1。


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

 

相关文章:

  • 2022-12-23
  • 2021-12-03
  • 2021-11-13
  • 2021-12-02
  • 2021-05-18
  • 2021-05-07
  • 2021-11-12
猜你喜欢
  • 2022-12-23
  • 2021-11-21
  • 2022-01-27
  • 2021-10-28
  • 2022-12-23
  • 2021-11-14
相关资源
相似解决方案