Description
https://leetcode.com/problems/minimum-depth-of-binary-tree/
Solving Ideas
递归
- 若为根结点为空,则返回0;
- 若左子树为空,则返回右子树的最小深度+1;
- 若右子树为空,则返回左子树的最小深度+1;
- 否则取左、右子树最小深度的较小值+1。
Solution
class Solution {
public int minDepth(TreeNode root) {
if(root == null) return 0;
if(root.left == null) return 1 + minDepth(root.right);
if(root.right == null) return 1 + minDepth(root.left);
return 1 + Math.min(minDepth(root.left), minDepth(root.right));
}
}