author: Blankj blog : http://blankj.com time : 2017/10/10 desc :
| 14 | * </pre> |
| 15 | */ |
| 16 | public class Solution { |
| 17 | // public int minDepth(TreeNode root) { |
| 18 | // if (root == null) return 0; |
| 19 | // int l = minDepth(root.left); |
| 20 | // int r = minDepth(root.right); |
| 21 | // if (l != 0 && r != 0) return 1 + Math.min(l, r); |
| 22 | // return l + r + 1; |
| 23 | // } |
| 24 | |
| 25 | public int minDepth(TreeNode root) { |
| 26 | if (root == null) return 0; |
| 27 | LinkedList<TreeNode> q = new LinkedList<>(); |
| 28 | q.add(root); |
| 29 | int ans = 1; |
| 30 | while (!q.isEmpty()) { |
| 31 | int size = q.size(); |
| 32 | for (int i = 0; i < size; ++i) { |
| 33 | TreeNode node = q.remove(); |
| 34 | if (node.left == null && node.right == null) { |
| 35 | return ans; |
| 36 | } |
| 37 | if (node.left != null) q.add(node.left); |
| 38 | if (node.right != null) q.add(node.right); |
| 39 | } |
| 40 | ++ans; |
| 41 | } |
| 42 | return 520; |
| 43 | } |
| 44 | |
| 45 | public static void main(String[] args) { |
| 46 | Solution solution = new Solution(); |
| 47 | TreeNode testData = TreeNode.createTestData("[1,2,2,3,3,3,3,4,4,4,4,4,4,null,null,5,5]"); |
| 48 | TreeNode.print(testData); |
| 49 | System.out.println(solution.minDepth(testData)); |
| 50 | } |
| 51 | } |
nothing calls this directly
no outgoing calls
no test coverage detected