-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathminimum_depth_of_binary_tree.dart
104 lines (76 loc) · 2.96 KB
/
minimum_depth_of_binary_tree.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
-* Minimum Depth of Binary Tree *-
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
Note: A leaf is a node with no children.
Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: 2
Example 2:
Input: root = [2,null,3,null,4,null,5,null,6]
Output: 5
Constraints:
The number of nodes in the tree is in the range [0, 105].
-1000 <= Node.val <= 1000
*/
// Definition for a binary tree node.
import 'dart:math';
class TreeNode {
int val;
TreeNode? left;
TreeNode? right;
TreeNode([this.val = 0, this.left, this.right]);
}
// Time complexity: O(n), Space complexity: O(h), h is tree height
class A {
// Runtime: 854 ms, faster than 20.00% of Dart online submissions for Minimum Depth of Binary Tree.
// Memory Usage: 184.5 MB, less than 60.00% of Dart online submissions for Minimum Depth of Binary Tree.
int minDepth(TreeNode? root) {
if (root == null) return 0;
int left = minDepth(root.left);
int right = minDepth(root.right);
//if both left and right 0, that means there is no path in left and right
// return 1 as current node in the current path
if (left == 0 && right == 0)
return 1;
// if left is 0 and right has some value, that means there is no path in left side
//but some path in right side, so return whatever value + 1, the extra 1 is for current node
else if (left == 0)
return right + 1;
// if right is 0 and left has some value, that means there is no path in right side
//but some path in left side, so return whatever value + 1, the extra 1 is for current node
else if (right == 0)
return left + 1;
// if in both side path exist, then consider the minimum and add 1 for the current node
else
return min(left, right) + 1;
}
}
class B {
// 52 / 52 test cases passed, but took too long.
// Status: Time Limit Exceeded
// Submitted: 0 minutes ago
int minDepth(TreeNode? root) {
if (root == null) return 0;
if (root.left == null && root.right == null) return 1;
int leftDepth =
root.left != null ? minDepth(root.left) : double.maxFinite.floor();
int rightDepth =
root.right != null ? minDepth(root.right) : double.maxFinite.floor();
return 1 + min(leftDepth, rightDepth);
}
}
class C {
// Runtime: 687 ms, faster than 20.00% of Dart online submissions for Minimum Depth of Binary Tree.
// Memory Usage: 184.3 MB, less than 80.00% of Dart online submissions for Minimum Depth of Binary Tree.
int minDepth(TreeNode? root) {
if (root == null) return 0;
// Base case
if (root.left == null && root.right == null) return 1;
// If left subtree is NULL, recursion for right subtree
if (root.left == null) return minDepth(root.right) + 1;
// If right subtree is NULL, recursion for left subtree
if (root.right == null) return minDepth(root.left) + 1;
return min(minDepth(root.left), minDepth(root.right)) + 1;
}
}