二叉树最小深度

ivy吖

关注

阅读 60

2022-02-22

二叉树最小深度

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

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)

0 0 举报