package com.tree;
//求二叉树的深度
public class TreeDepth {
public static int getTreeDepth(TreeNode root){
if(root==null) return 0;
int left = getTreeDepth(root.left);
int right = getTreeDepth(root.right);
return right > left ? right + 1 : left + 1;
}
private class TreeNode {
private TreeNode left;
private TreeNode right;
private Integer val;
public TreeNode(Integer val) {
this.val = val;
}
}
}
二叉树的深度
最新推荐文章于 2021-12-18 15:59:21 发布