Skip to content

Commit a5de8c6

Browse files
authored
Merge pull request #27 from fartem/111_Minimum_Depth_of_Binary_Tree
2023-02-15 update: added "111. Minimum Depth of Binary Tree"
2 parents b4c8e1a + e016fbf commit a5de8c6

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
3535
| 104. Maximum Depth of Binary Tree | [Link](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [Link](./lib/easy/maximum_depth_of_binary_tree.dart) |
3636
| 108. Convert Sorted Array to Binary Search Tree | [Link](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) | [Link](./lib/easy/convert_sorted_array_to_binary_search_tree.dart) |
3737
| 110. Balanced Binary Tree | [Link](https://leetcode.com/problems/balanced-binary-tree/) | [Link](./lib/easy/balanced_binary_tree.dart) |
38+
| 111. Minimum Depth of Binary Tree | [Link](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [Link](./lib/easy/minimum_depth_of_binary_tree.dart) |
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import 'dart:math';
2+
3+
import '../common/tree_node.dart';
4+
5+
// https://leetcode.com/problems/minimum-depth-of-binary-tree/
6+
class Solution {
7+
int minDepth(TreeNode? root) {
8+
if (root != null) {
9+
if (root.left == null && root.right == null) {
10+
return 1;
11+
} else if (root.left == null) {
12+
return minDepth(root.right) + 1;
13+
} else if (root.right == null) {
14+
return minDepth(root.left) + 1;
15+
}
16+
return min(
17+
minDepth(root.left) + 1,
18+
minDepth(root.right) + 1,
19+
);
20+
}
21+
return 0;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import 'package:leetcode_dart/common/tree_node.dart';
2+
import 'package:leetcode_dart/easy/minimum_depth_of_binary_tree.dart';
3+
import 'package:test/test.dart';
4+
5+
void main() {
6+
group(
7+
'Example tests',
8+
() {
9+
final mdobt = Solution();
10+
test(
11+
'2',
12+
() => expect(
13+
2,
14+
mdobt.minDepth(
15+
TreeNode(
16+
3,
17+
TreeNode(9),
18+
TreeNode(
19+
20,
20+
TreeNode(15),
21+
TreeNode(7),
22+
),
23+
),
24+
),
25+
),
26+
);
27+
test(
28+
'5',
29+
() => expect(
30+
5,
31+
mdobt.minDepth(
32+
TreeNode(
33+
2,
34+
null,
35+
TreeNode(
36+
3,
37+
null,
38+
TreeNode(
39+
4,
40+
null,
41+
TreeNode(
42+
5,
43+
null,
44+
TreeNode(6),
45+
),
46+
),
47+
),
48+
),
49+
),
50+
),
51+
);
52+
},
53+
);
54+
}

0 commit comments

Comments
 (0)