首页 > 其他分享 >Leecode 111.二叉树的最小深度

Leecode 111.二叉树的最小深度

时间:2022-10-11 08:22:15浏览次数:46  
标签:node right TreeNode val int Leecode 111 二叉树 que

 /*
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int minDepth(TreeNode root) {
        //先判断是否为空
        if(root == null) {
            return 0;
        }
        int depth=0;
        Queue<TreeNode> que = new LinkedList<>();
        que.offer(root);
        while (!que.isEmpty()) {
            int len  = que.size();
            depth ++;
            for (int i = 0; i < len; i++) {
                TreeNode node = que.poll();
       //判断两个子节点都为空则为叶子节点 if(node.right == null && node.left == null ){ return depth; } if(node.left != null) {que.offer(node.left);} if(node.right != null) {que.offer(node.right);} } } return depth; } }

  

标签:node,right,TreeNode,val,int,Leecode,111,二叉树,que
From: https://www.cnblogs.com/noedaydayup/p/16778010.html

相关文章