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.

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
import java.util.*;
public class Solution {
    public int run(TreeNode root) {
        if(root == null){
            return 0;
        }
        Queue<TreeNode> s  = new LinkedList<TreeNode>();
        s.add(root);
        int start = 0;
        int end = 1;
        int level = 1;
        while(s!=null){
            TreeNode node = s.poll();
            start++;
            if(node.left == null && node.right == null){
                return level;
            }
            if(node.left!=null){
                s.add(node.left);
            }
            if(node.right!=null){
                s.add(node.right);
            }
            if(start == end){
                start = 0;
                level++;
                end = s.size();
            }
        }
        return level;
    }
}

原文地址:https://www.cnblogs.com/q-1993/p/10990601.html

时间: 2024-10-07 14:02:21

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笔记: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. 二. 题目分析 这道题属于二叉树的深度优先搜索,然后返回深度最小的值,可以递归(当然,也可以使用迭代)来实现.递归退出的条件是到达叶子节点或者到达空子树,使用空子树

[LeetCode] 111. Minimum Depth of Binary Tree 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. 题意及分析:求一棵二叉树的最低高度,即根节点到叶子节点的最短路径.遍历二叉树,然后用一个变量保存遍历到当前节点的最小路径即可,然后每次遇到叶子节点,就判断当前叶节点高度和先

Minimum Depth of Binary Tree

1. Title 2. Http address https://leetcode.com/problems/minimum-depth-of-binary-tree/ 3. The question 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

111. Minimum Depth of Binary Tree java solutions

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 1 /**

35: Minimum Depth of Binary Tree

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

[LeetCode#104, 111]Maximum Depth of Binary Tree, Minimum Depth of Binary Tree

The problem 1: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. My analysis: The recursion solution for this problem is very elegant!It inc

LeetCode OJ - Minimum &amp;&amp; Maximum Depth of Binary Tree

这两道题用递归的解法都很简单,只是稍有不同. 下面是AC代码: 1 /** 2 * Given a binary tree, find its minimum depth. 3 * the minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 4 * @param root 5 * @return 6 */ 7 public in

Java [Leetcode 111]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. 解题思路: 递归. 代码描述: /** * Definition for a binary tree node. * public class TreeNode { *

Java for LeetCode 111 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. 解题思路:注意minimum depth最后遍历的那个点,left right都必须为null,JAVA实现如下: public int minDepth(TreeNode roo