Minimum Depth of Binary Tree,求树的最小深度

算法分析:递归和非递归两种方法。

public class MinimumDepthofBinaryTree {

	//递归,树的最小深度,就是它左右子树的最小深度的最小值+1
	public int minDepth(TreeNode root) {
        if(root == null)
        {
        	return 0;
        }
        int lmin = minDepth(root.left);
        int rmin = minDepth(root.right);
        if(lmin == 0 && rmin == 0)
        {
        	return 1;
        }
        if(lmin == 0)//左子树为空,右子树不为空,则最小深度为右子子树的最小深度+1
        {
        	return rmin + 1;
        }
        if(rmin == 0)
        {
        	return lmin + 1;
        }
        return Math.min(lmin, rmin)+1;
    }

	//非递归,按层遍历
	public int minDepth2(TreeNode root) {
		if(root == null)
		{
			return 0;
		}
		ArrayList<TreeNode> list = new ArrayList<>();
		int count = 1;
		list.add(root);
		while(!list.isEmpty())
		{
			ArrayList<TreeNode> temp = new ArrayList<>();
			for (TreeNode node : list) {
				if(node.left == null && node.right == null)//当节点左右子树都为空时,该节点的深度即为树的最小深度
				{
					return count;
				}
				if(node.left != null)
				{
					temp.add(node.left);
				}
				if(node.right != null)
				{
					temp.add(node.right);
				}
			}
			count ++;//按层遍历,每循环一次,就是一层,层数加1
			list = temp;
		}
		return count;
    }
}

  

时间: 2024-11-08 01:14:40

Minimum Depth of Binary Tree,求树的最小深度的相关文章

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

[111-Minimum Depth of Binary Tree(二叉树的最小深度)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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. 题目大意 给定

LeetCode Maximum Depth of Binary Tree (求树的深度)

题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 *

35: Minimum Depth of Binary Tree

/************************************************************************/        /*       35:     Minimum Depth of Binary Tree                                         */        /***********************************************************************

[leetcode]Minimum Depth of Binary Tree @ Python

原题地址:http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ 题意: 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. 解题思路:分几种情况考虑:1,树为空,

LeetCode My Solution: Minimum Depth of Binary Tree

Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions 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.

[Leetcode][JAVA] Minimum Depth of Binary Tree &amp;&amp; Balanced Binary Tree

Minimum Depth of Binary Tree 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. 比较左子树和右子树的深度,取小的那个返回上来,并+1. 需要注意的是如果没有左子树或右子树.那么无条件取存在的那一边子树深

leetcode dfs Minimum Depth of Binary Tree

Minimum Depth of Binary Tree Total Accepted: 25609 Total Submissions: 86491My Submissions 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.

Minimum Depth of Binary Tree leetcode java

题目: 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. 题解: 递归解法急速判断左右两边子树哪个depth最小,要注意如果有个节点只有一边孩子时,不能返回0,要返回另外一半边的depth. 递归解法: 1     public 

Leetcode 111. Minimum Depth of Binary Tree

111. Minimum Depth of Binary Tree Total Accepted: 114343 Total Submissions:368035 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 le

leetcode -day17 Path Sum I II &amp; Flatten Binary Tree to Linked List &amp; Minimum Depth of Binary Tree

1.  Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 r