LeetCode Minimum Depth of Binary Tree

原创 2015年07月07日 09:59:09

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:

DFS即可。

import java.util.*;

public class Solution {

	public int minDepth(TreeNode root) {
		if (root == null)
			return 0;
		return depth(root);
	}

	int depth(TreeNode root) {
		// if (root.left == null && root.right == null)
		// return 1;
		int ret = Integer.MAX_VALUE;
		if (root.left != null)
			ret = Math.min(depth(root.left), ret);
		if (root.right != null)
			ret = Math.min(depth(root.right), ret);

		if (ret == Integer.MAX_VALUE)
			ret = 1;
		else
			ret++;

		return ret;
	}
}


【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】

【111-Minimum Depth of Binary Tree(二叉树的最小深度)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a binary tre...

LeetCode-Minimum Depth of Binary Tree

作者:disappearedgod 文章出处:http://blog.csdn.net/disappearedgod/article/details/23621903 时间:2014-6-7 ...

leetCode #111 - Minimum Depth of Binary Tree

题目描述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along t...
  • LawFile
  • LawFile
  • 2016年11月03日 23:09
  • 111

[leetcode]111.Minimum Depth of Binary Tree

111. Minimum Depth of Binary TreeGiven a binary tree, find its minimum depth. The minimum depth is ...

[leetcode] 111. Minimum Depth of Binary Tree 解题报告

题目链接:https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its min...

LeetCode 111.Minimum Depth of Binary Tree

题目大意是求一颗树的最小深度。从根节点到叶子节点每往下一层,深度加1,根节点的深度为1。       先贴代码:       int m = 9999; int depth = 1; void d...

<LeetCode OJ> (104 / 111) Maximum & Minimum Depth of Binary Tree

Maximum Depth of Binary Tree My Submissions Question Total Accepted: 107329 Total Submission...

【leetcode】:Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the short...
  • WX_East
  • WX_East
  • 2017年04月27日 17:23
  • 95

leetcode Minimum Depth of Binary Tree C++题解

题目描述e.给出一个二叉树,求其最短路径长度,最短路径定义长度:从根节点到最近的叶子节点的路径上的节点个数。...

[Leetcode] Minimum Depth of Binary Tree

本题目我自己编写了一个代码,但是运行速度很慢,看了别人的代买才知道,有些树的递归逻辑的判断并不需要一定在本次执行,如果函数内部写了异常处理即树root为空,则对叶子节点的判断可以由自身的返回值判断,这...
  • daniren
  • daniren
  • 2016年10月04日 10:17
  • 95
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode Minimum Depth of Binary Tree
举报原因:
原因补充:

(最多只允许输入30个字)