public int minDepth(TreeNode root) {
return dfs(root);
}
int dfs(TreeNode root ){
if (root == null) return 0;
if (root.left == null && root.right == null){
return 1;
}
if (root.left != null && root.right != null){
return Math.min(dfs(root.left)+1,dfs(root.right)+1);
}else if (root.left != null){
return dfs(root.left)+1;
}else {
return dfs(root.right)+1;
}
}
public int minDepth(TreeNode root) {
if (root == null){
return 0;
}
if (root.left == null && root.right == null){
return 1;
}
int min_depth = Integer.MAX_VALUE;
if (root.left != null){
min_depth = Math.min(min_depth,minDepth(root.left));
}
if (root.right != null){
min_depth = Math.min(min_depth,minDepth(root.right));
}
return min_depth+1;
}