leetcode Minimum Depth of Binary Tree (平衡树的最小深度)

leetcode 题目:https://leetcode.com/problems/minimum-depth-of-binary-tree/

Minimum Depth of Binary Tree ping 平衡树的最小深度

解题思路:

   1.递归求解平衡树的最小高度

  2. 一定注意 :左子树为空时,平衡树的最小高度=右子树的最小高度+1,右子树为空时,平衡树的最小高度=左子树的最小高度+1

    /**
	 * 计算平衡树的最小高度
	 * @param root
	 * @return
	 */
	public  static int minDepth(TreeNode root) {
		if(root==null){
			return 0;
		}
		if(root.left==null){
			return minDepth(root.right)+1;
		}
		if(root.right==null){
			return minDepth(root.left)+1;
		}
		return Math.min(minDepth(root.left),minDepth(root.right))+1;
	}

  

猜你喜欢

转载自blog.csdn.net/u011243684/article/details/84765770