LeetCode OJ Minimum Depth of Binary Tree 递归求解

    题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/

111. Minimum Depth of Binary Tree

My Submissions
Total Accepted: 94580 Total Submissions: 312802 Difficulty: Easy

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.

Subscribe to see which companies asked this question

Show Tags
Show Similar Problems
Have you met this question in a real interview? 
Yes
 
No

Discuss

    求一棵二叉树。离根近期的叶子的高度。递归找出两棵子树的最小高度加一便可求解。

    我的AC代码

public class MinimumDepthofBinaryTree {

	public int minDepth(TreeNode root) {
		if(root == null) return 0;
        return height(root);
    }
	
	private int height(TreeNode root){
		if(root.left == null && root.right == null) return 1;
		else if(root.left == null) return height(root.right) + 1;
		else if(root.right == null) return height(root.left) + 1;
		return Math.min(height(root.left), height(root.right)) + 1;
	}
}


posted @ 2018-04-09 20:00  zhchoutai  阅读(105)  评论(0编辑  收藏  举报