Skip to content

Commit f1e7943

Browse files
committed
Time: N/A (0%), Space: N/A (0%) - LeetHub
1 parent b78309a commit f1e7943

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

LeetCode/Easy/0111-minimum-depth-of-binary-tree/0111-minimum-depth-of-binary-tree.java

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,38 @@
1717

1818
class Solution {
1919
Queue<TreeNode> queue = new ArrayDeque<>();
20+
int minDepth=0;
2021

2122
public int minDepth(TreeNode root) {
22-
if(root == null) return 0;
23+
if(root == null) return minDepth;
24+
//dfs
25+
// if(root.left==null&&root.right==null) return minDepth;
26+
if(root.left==null && root.right==null) return 1+minDepth;
27+
else if(root.left==null) return 1+minDepth(root.right);
28+
else if(root.right==null) return 1+minDepth(root.left);
29+
else return 1+Math.min(minDepth(root.left), minDepth(root.right));
2330

24-
queue.add(root);
25-
int minDepth = 0;
31+
// minDepth = 1+Math.min(minDepth(root.left), minDepth(root.right));
2632

27-
while(!queue.isEmpty()){
28-
minDepth++;
29-
int queueLen = queue.size();
33+
// return minDepth;
34+
// //bfs
35+
// if(root == null) return 0;
3036

31-
while(queueLen-->0){
32-
TreeNode node = queue.poll();
33-
if(node.left==null && node.right==null) return minDepth;
34-
if(node.left!=null) queue.add(node.left);
35-
if(node.right!=null) queue.add(node.right);
36-
}
37-
}
37+
// queue.add(root);
38+
// int minDepth = 0;
3839

39-
return minDepth;
40+
// while(!queue.isEmpty()){
41+
// minDepth++;
42+
// int queueLen = queue.size();
43+
44+
// while(queueLen-->0){
45+
// TreeNode node = queue.poll();
46+
// if(node.left==null && node.right==null) return minDepth;
47+
// if(node.left!=null) queue.add(node.left);
48+
// if(node.right!=null) queue.add(node.right);
49+
// }
50+
// }
51+
52+
// return minDepth;
4053
}
4154
}

0 commit comments

Comments
 (0)