0
点赞
收藏
分享

微信扫一扫

二叉树最小深度

ivy吖 2022-02-22 阅读 59

二叉树最小深度

递归计算,注意递归的跳出条件

class TreeNode {
	int val;
	TreeNode left;
	TreeNode right;
}

int getMinDepth(TreeNode root) {
	if (root == null) {
		 return 0;
	}
	return getMin(root);
}

int getMin(TreeNode root) {
	if (root == null){
		return Math.MAX_VALUE
	}
	if (root.left == null && root.right == null) {
		return 1;
	}
	return Math.min(getMin(root.left), getMin(root.right)) + 1;

}
举报

相关推荐

0 条评论