题目描述

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

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

56.剑指Offer-二叉树的深度

解题思路

public int TreeDepth(TreeNode root) {
    return root == null ? 0 : 1 + Math.max(TreeDepth(root.left), TreeDepth(root.right));
}
public static int FindDepth(TreeNode root){
	if(root==null){
		return 0;
	}
	int a=FindDepth(root.left);
	int b=FindDepth(root.right);
	return Math.max(a, b)+1;
}

 

相关文章:

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