Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the short

Posted q-1993

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the short相关的知识,希望对你有一定的参考价值。

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

 

以上是关于Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the short的主要内容,如果未能解决你的问题,请参考以下文章

[GeeksForGeeks] Remove all half nodes of a given binary tree

Convert a given binary tree to double linked list

Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary

Unique Binary Search Trees

Unique Binary Search Trees

树Unique Binary Search Trees