Minimum Depth of Binary Tree
题解
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* this.val = val;
* this.left = this.right = null;
* }
* }
public class Solution {
/**
* @param root: The root of binary tree.
* @return: An integer.
*/
public int minDepth(TreeNode root) {
if (root == null) return 0;
int leftDepth = minDepth(root.left);
// current node is not leaf node
if (root.left == null) {
return 1 + rightDepth;
} else if (root.right == null) {
return 1 + leftDepth;
}
return 1 + Math.min(leftDepth, rightDepth);