File tree Expand file tree Collapse file tree 1 file changed +27
-14
lines changed
LeetCode/Easy/0111-minimum-depth-of-binary-tree Expand file tree Collapse file tree 1 file changed +27
-14
lines changed Original file line number Diff line number Diff line change 1717
1818class 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}
You can’t perform that action at this time.
0 commit comments