LeetCode: Minimum Depth of Binary Tree 解题报告
2014-12-18 22:59
323 查看
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.
View Code
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/MinDepth_1218_2014.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.
// SOLUTION 2: // Level Traversal: public int minDepth(TreeNode root) { /* 主页君认为,在这应该是属于未定义行为,这里我们定义为MAX会比较好,因为 null就是取不到任何节点,没有path,不应该将最小值定为0. */ if (root == null) { return 0; } int level = 0; Queue<TreeNode> q = new LinkedList<TreeNode>(); q.offer(root); while (!q.isEmpty()) { int size = q.size(); level++; for (int i = 0; i < size; i++) { TreeNode cur = q.poll(); if (cur.left == null && cur.right == null) { return level; } if (cur.left != null) { q.offer(cur.left); } if (cur.right != null) { q.offer(cur.right); } } } return 0; }
View Code
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/MinDepth_1218_2014.java
相关文章推荐
- 【LeetCode】Minimum Depth of Binary Tree 解题报告
- LeetCode(111) Minimum Depth of Binary Tree解题报告
- 【LeetCode】Maximum Depth of Binary Tree 解题报告
- Maximum Depth of Binary Tree | leetcode 104 【Java解题报告】
- 【leetcode】Minimum Depth of Binary Tree
- [LeetCode 111] - 二叉树的最小深度 (Minimum Depth of Binary Tree)
- [leetcode] Minimum Depth of Binary Tree
- 【LeetCode with Python】 Minimum Depth of Binary Tree
- LeetCode:Minimum Depth of Binary Tree
- [LeetCode] Minimum Depth of Binary Tree - BFS
- LeetCode_Minimum Depth of Binary Tree
- [leetcode]Minimum Depth of Binary Tree
- [LeetCode]Minimum Depth of Binary Tree
- leetcode 31: Minimum Depth of Binary Tree
- [LeetCode] Minimum Depth of Binary Tree - DFS
- [Leetcode 10] 111 Minimum Depth of Binary Tree
- [leetcode]Minimum Depth of Binary Tree
- [Leetcode] Minimum Depth of Binary Tree
- LeetCode: Minimum Depth of Binary Tree
- leetcode 31: Minimum Depth of Binary Tree