【LeetCode】111.Minimum Depth of Binary Tree(Easy)解题报告
题目地址:https://leetcode.com/problems/minimum-depth-of-binary-tree/description/
题目描述:
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.
二叉树最小深度
Solution:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
返回二叉树的最小深度
time : O(n)
space : O(n)
*/
class Solution {
public int minDepth(TreeNode root) {
if(root == null) return 0;
if(root.left == null || root.right == null){
return Math.max(minDepth(root.left),minDepth(root.right))+1;
}
return Math.min(minDepth(root.left),minDepth(root.right))+1;
}
}
Date:2018年3月11日