* Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ publicclass{ publicint ans = Integer.MAX_VALUE;
publicintminDepth(TreeNode root){ if (root == null) return0; if (root.left == null && root.right == null) {
return1; } minDepth(root,0); return ans; }
publicvoidminDepth(TreeNode root, int d){
if (d >= ans) return; if (root.left == null && root.right == null) { ans = Math.min(d + 1, ans); } if (root.left != null) { minDepth(root.left, d + 1); }
if (root.right != null) { minDepth(root.right, d + 1); }
近期评论