题目描述

输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。

 

题目分析

树的深度=左子树的深度和右子树深度中最大者+1

 

代码

function TreeDepth(pRoot) {
  if (pRoot === null) return 0;
  const leftDep = TreeDepth(pRoot.left);
  const rightDep = TreeDepth(pRoot.right);
  return Math.max(leftDep, rightDep) + 1;
}

 

相关文章:

  • 2022-01-09
  • 2022-12-23
  • 2021-06-25
  • 2022-02-05
  • 2021-09-15
  • 2021-07-16
猜你喜欢
  • 2021-12-13
  • 2021-10-01
  • 2022-12-23
  • 2022-02-11
  • 2022-12-23
  • 2021-09-17
  • 2021-06-04
相关资源
相似解决方案