MCPcopy Create free account
hub / github.com/Blankj/awesome-java-leetcode / minDepth

Method minDepth

src/com/blankj/easy/_0111/Solution.java:25–43  ·  view source on GitHub ↗
(TreeNode root)

Source from the content-addressed store, hash-verified

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();

Callers 1

mainMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected