/** * DeFinition of TreeNode: 大专栏Minimum Depth of Binary Tree * public class TreeNode { * public int val; * public TreeNode left,right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public @H_502_311@classSolution{ /** * @param root: The root of binary tree. * @return: An integer. */ publicintminDepth(TreeNode root){ if (root == null) return0;
int leftDepth = minDepth(root.left); int rightDepth = minDepth(root.right);
// current node is not leaf node if (root.left == null) { return1 + rightDepth; } elseif (root.right == null) { return1 + leftDepth; }