LeetCode@111_Minimum_Depth_of_Binary_Tree
来源:互联网 发布:网络歌手大赛活动方案 编辑:程序博客网 时间:2024/06/05 03:10
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.
java:
/** * 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<TreeNode> queue = new LinkedList<>();Queue<Integer> depth = new LinkedList<>();queue.add(root);depth.add(1);while (!queue.isEmpty()){TreeNode currNode = queue.poll();int currDepth = depth.poll();if (currNode.left != null){queue.add(currNode.left);depth.add(currDepth+1);}if (currNode.right != null){queue.add(currNode.right);depth.add(currDepth+1);}if (currNode.left == null && currNode.right == null){return currDepth;}}return -1;}}
阅读全文
0 0
- LeetCode@111_Minimum_Depth_of_Binary_Tree
- Leetcode 111
- leetcode 111: Distinct Subsequences
- leetcode #111 in cpp
- Leetcode no. 111
- leetCode练习(111)
- [Leetcode] 95, 109, 111
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- 设计模式-工厂模式
- 光电耦合器电路应用符号
- Python安装scrapy库过程中出现 " Failed building wheel for Twisted"
- java script入门
- 【Java高级开发工程师】近一个月的面试总结
- LeetCode@111_Minimum_Depth_of_Binary_Tree
- IDEA中写jsp代码没有提示
- Linux 内存管理浅析
- android知识点收集
- mysql explain中key_len的作用
- linux内核调度策略SCHED_OTHER,SCHED_FIFO和SCHED_RR
- intellij idea 工具栏的隐藏和显示
- 获取一段时间内数据
- SPRINGMYBATIS01 Unit02: 参数值注入 、 基于注解的组件扫描