题解 | 二叉树的最小深度
二叉树的最小深度
https://www.nowcoder.com/practice/e08819cfdeb34985a8de9c4e6562e724
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* }
*/
public class Solution {
/**
*
* @param root TreeNode类
* @return int整型
*/
public int run (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.max(run(root.left), run(root.right)) + 1;
}
return Math.min(run(root.left), run(root.right)) + 1;
}
}
查看16道真题和解析
