?LeetCode刷題實戰(zhàn)111:二叉樹的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Note: A leaf is a node with no children.
題意

解題
public?class?Solution?{
????public?int?minDepth(TreeNode root) {
????if(root!=null){
????????int?left=Integer.MAX_VALUE;
????????int?right=Integer.MAX_VALUE;
????????if(root.left!=null){
????????????left=minDepth(root.left);
????????}
????????if(root.right!=null){
????????????right=minDepth(root.right);
????????}
????????if(left????????????return?left+1;
????????}
????????else?if(left>right){
????????????return?right+1;
????????}
????????else?if(left==right&&left!=Integer.MAX_VALUE){
????????????return?left+1;
????????}
????????else{
????????????return?1;
????????}
????????
????}
?????return?0;
????}
}
評論
圖片
表情
