111. Minimum Depth of Binary Tree
来源:互联网 发布:手机唱歌调音软件 编辑:程序博客网 时间:2024/04/28 17:12
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 1 DFS
1ms 12.55
public class Solution { public int minDepth(TreeNode root) { if(root == null){ return 0; } return getMin(root); } public int getMin(TreeNode root){ if(root == null){ return Integer.MAX_VALUE; } if(root.left == null && root.right == null){ return 1; } return Math.min(getMin(root.left), getMin(root.right)) + 1; }}Solution 2 BFS
public class Solution { public int minDepth(TreeNode root) { if(root == null){ return 0; } Queue<TreeNode> q = new LinkedList<TreeNode>(); int depth = 1; q.offer(root); while(!q.isEmpty()){ int size = q.size(); for(int i = 0; i < size; i++){ TreeNode temp = q.poll(); if(temp.left != null){ q.offer(temp.left); } if(temp.right != null){ q.offer(temp.right); } if(temp.left == null && temp.right == null){ return depth; } } depth++; } return depth; }}
0 0
- 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
- 111.Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- [leetcode] 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree LeetCode
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 【LeetCode】111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- LeetCode *** 111. Minimum Depth of Binary Tree
- 111. Minimum Depth of Binary Tree
- HDU 5494 Card Game
- 集训队专题(3)1013 最短路径问题
- HDU 5210 Delete
- 【POJ】[2236]Wireless Network
- CLLocationManager无法定位问题,CLLocationManagerDelegate不调用didUpdateLocations
- 111. Minimum Depth of Binary Tree
- 【POJ】[1611]The Suspects
- Boost Graph
- uva 991 Safe Salutations
- Python学习之面向对象
- 110. Balanced Binary Tree
- Python元编程-遗忘的远古凶兽
- quick-cocos2dx scheduler.scheduleGlobal坑
- POJ 3074 Sudoku(数独|Dancing Links精确覆盖)