博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
111. Minimum Depth of Binary Tree
阅读量:4705 次
发布时间:2019-06-10

本文共 1015 字,大约阅读时间需要 3 分钟。

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public int minDepth(TreeNode root) {        if(root==null)            return 0;        Queue
q=new LinkedList
(); q.offer(root); int size=q.size(); int res=1; int count=0; while(!q.isEmpty()) { TreeNode temp=q.poll(); if(temp.left==null&&temp.right==null) { //找到了叶子节点 break; } if(temp.left!=null) q.offer(temp.left); if(temp.right!=null) q.offer(temp.right); count++; if(count==size) { count=0; size=q.size(); res++; } } return res; }}

 

转载于:https://www.cnblogs.com/aguai1992/p/5349527.html

你可能感兴趣的文章
记一次Linux服务器上查杀木马经历
查看>>
Winform Timer用法,Invoke在Timer的事件中更新控件状态
查看>>
多线程同步技术(一)
查看>>
HDU 1829 并查集up
查看>>
rabbitmq3.7集群搭建实战
查看>>
深入理解Asp.net MVC路由
查看>>
正则化方法:L1和L2 regularization、数据集扩增、dropout
查看>>
java是编译型语言还是解释型语言?
查看>>
day4 小结
查看>>
语系/地区码
查看>>
【非原创】LightOJ - 1284 Lights inside 3D Grid【概率期望】
查看>>
【前端优化之拆分CSS】前端三剑客的分分合合
查看>>
Sublime Python3编译环境修改
查看>>
Linux系统修改防火墙配置
查看>>
cocos2d 触屏事件
查看>>
技术人生:他们的毅力超过才力
查看>>
ibatis.net:在VS中支持xml智能提示
查看>>
spring+springmvc+mybaties整合实例
查看>>
window.open 子窗口关闭刷新父页面
查看>>
Hibernate的几种主键生成策略
查看>>