【Leetcode-easy-111】Minimum Depth of Binary Tree
2017-10-14 19:21
447 查看
【Leetcode-easy-111】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.
思路
层次遍历二叉树程序
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public int minDepth(TreeNode root) { if (root == null) return 0; // if (root.left == null || root.right == null) return 1; LinkedList<TreeNode> queue = new LinkedList<>(); queue.offer(root); int level = 0; while (!queue.isEmpty()){ level ++; int len = queue.size(); for (int i = 0; i < len; i ++){ TreeNode currNode = queue.poll(); if (currNode.left == null && currNode.right == null) return level; if (currNode.left != null) queue.offer(currNode.left); if (currNode.right != null) queue.offer(currNode.right); } } return level; } }
相关文章推荐
- [Leetcode 111, Easy] Minimum Depth of Binary Tree
- LeetCode:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy
- 【LeetCode】Minimum Depth Of Binary Tree二叉树的最小深度 - Easy
- leetcode[111]:Minimum Depth of Binary Tree
- LeetCode 111 Minimum Depth of Binary Tree(二叉树的最短深度)(BT、DFS)(*)
- leetcode 111 Minimum Depth of Binary Tree
- LeetCode(111) Minimum Depth of Binary Tree
- LeetCode_111 Minimum Depth of Binary Tree
- 【leetcode】【111】Minimum Depth of Binary Tree
- leetcode 111: Minimum Depth of Binary Tree
- <LeetCode OJ> (104 / 111) Maximum & Minimum Depth of Binary Tree
- 【LEETCODE】111-Minimum Depth of Binary Tree
- [leetcode-111]Minimum Depth of Binary Tree(C语言)
- Leetcode 111 Minimum Depth of Binary Tree
- 【leetcode c++】111 Minimum Depth of Binary Tree
- 【LeetCode 111_二叉树_遍历】Minimum Depth of Binary Tree
- leetcode 111:Minimum Depth of Binary Tree
- LeetCode #111:Minimum Depth of Binary Tree
- [leetcode 111]Minimum Depth of Binary Tree
- 【LeetCode-111】Minimum Depth of Binary Tree(C++)