Skip to content

Commit 1f8c249

Browse files
authored
Added complexity tags 101-136
1 parent 6f1b3a1 commit 1f8c249

File tree

20 files changed

+251
-10
lines changed

20 files changed

+251
-10
lines changed

src.save/main/java/g0101_0200/s0101_symmetric_tree/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Breadth_First_Search
44
// #Tree #Binary_Tree #Data_Structure_I_Day_11_Tree #Level_2_Day_15_Tree
5-
// #2022_06_22_Time_0_ms_(100.00%)_Space_42.3_MB_(46.67%)
5+
// #Big_O_Time_O(N)_Space_O(log(N)) #2022_06_22_Time_0_ms_(100.00%)_Space_42.3_MB_(46.67%)
66

77
import com_github_leetcode.TreeNode;
88

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Time Complexity (Big O Time):**
2+
3+
The program uses a recursive approach to check if the binary tree is symmetric. It recursively compares the left subtree of the root with the right subtree of the root. Here's the analysis of time complexity:
4+
5+
- In the worst case, the program visits each node once. This happens when the tree is perfectly symmetric (a mirror image of itself), and the program checks every node.
6+
7+
- Therefore, the time complexity of this program is O(N), where N is the number of nodes in the binary tree.
8+
9+
**Space Complexity (Big O Space):**
10+
11+
The space complexity of the program is determined by the space required for the recursive call stack and the space needed for the function arguments and variables. Here's the analysis of space complexity:
12+
13+
- The main function `isSymmetric` has a constant amount of space overhead. It doesn't use any additional data structures or arrays that depend on the input size.
14+
15+
- The recursive function `helper` is called recursively for each pair of nodes in the tree. In the worst case, when the binary tree is perfectly symmetric, the maximum depth of the call stack is equal to the height of the tree. In a balanced tree, this height is log(N), where N is the number of nodes.
16+
17+
- Therefore, the space complexity due to the call stack is O(log(N)) in the best-case scenario (balanced tree) and O(N) in the worst-case scenario (skewed tree).
18+
19+
- The space used for function arguments and variables (e.g., `leftNode`, `rightNode`) is constant and does not depend on the input size.
20+
21+
- Overall, the space complexity of the program is O(log(N)) in the best-case scenario and O(N) in the worst-case scenario. The space complexity is mainly determined by the depth of the call stack when checking a perfectly symmetric tree.
22+
23+
In summary, the time complexity of the program is O(N), where N is the number of nodes in the binary tree, and the space complexity is O(log(N)) in the best-case scenario (perfectly symmetric tree) and O(N) in the worst-case scenario (skewed tree). The space complexity is primarily determined by the depth of the call stack when checking a symmetric tree.

src.save/main/java/g0101_0200/s0102_binary_tree_level_order_traversal/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Breadth_First_Search #Tree
44
// #Binary_Tree #Data_Structure_I_Day_11_Tree #Level_1_Day_6_Tree #Udemy_Tree_Stack_Queue
5-
// #2022_06_22_Time_1_ms_(91.09%)_Space_43.6_MB_(42.50%)
5+
// #Big_O_Time_O(N)_Space_O(N) #2022_06_22_Time_1_ms_(91.09%)_Space_43.6_MB_(42.50%)
66

77
import com_github_leetcode.TreeNode;
88
import java.util.ArrayList;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
**Time Complexity (Big O Time):**
2+
3+
The program uses a level order traversal approach, which visits each node once, and each edge is traversed exactly twice (once from parent to child and once from child to parent). Here's the analysis of time complexity:
4+
5+
- In the worst case, when the binary tree is completely unbalanced (e.g., a skewed tree), the program needs to visit all N nodes in the tree.
6+
7+
- During the traversal, the program processes each node once and performs constant-time operations (addition and removal from the queue) for each node.
8+
9+
- Therefore, the time complexity of this program is O(N), where N is the number of nodes in the binary tree.
10+
11+
**Space Complexity (Big O Space):**
12+
13+
The space complexity of the program is determined by the space required for data structures, specifically the queue used for the level order traversal, and the space needed for the output list of lists. Here's the analysis of space complexity:
14+
15+
- The program uses a queue to perform level order traversal. In the worst case, when the binary tree is completely unbalanced (e.g., a skewed tree), the queue can contain all N nodes in the tree. Therefore, the space complexity due to the queue is O(N).
16+
17+
- The program constructs a list of lists (`result`) to store the level order traversal result. In the worst case, the result list can contain N/2 lists (for a completely unbalanced tree), each with an average of 2 elements (at the lowest level of the tree).
18+
19+
- Therefore, the space complexity due to the `result` list is also O(N).
20+
21+
- Additional space is used for temporary variables such as `level` and loop control variables, but this space is constant and does not depend on the input size.
22+
23+
- Overall, the space complexity of the program is O(N) due to the space required for the queue and the level order traversal result.
24+
25+
In summary, the time complexity of the program is O(N) because it visits each node once, and the space complexity is also O(N) due to the space required for the queue and the level order traversal result. The space complexity is primarily determined by the size of the queue when traversing a completely unbalanced tree.

src.save/main/java/g0101_0200/s0104_maximum_depth_of_binary_tree/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Breadth_First_Search
44
// #Tree #Binary_Tree #Data_Structure_I_Day_11_Tree
55
// #Programming_Skills_I_Day_10_Linked_List_and_Tree #Udemy_Tree_Stack_Queue
6-
// #2022_06_22_Time_0_ms_(100.00%)_Space_42.9_MB_(67.03%)
6+
// #Big_O_Time_O(N)_Space_O(H) #2022_06_22_Time_0_ms_(100.00%)_Space_42.9_MB_(67.03%)
77

88
import com_github_leetcode.TreeNode;
99

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
**Time Complexity (Big O Time):**
2+
3+
The program uses a recursive approach to traverse the binary tree and find its maximum depth. The time complexity can be analyzed as follows:
4+
5+
- In the worst case, when the binary tree is completely unbalanced (e.g., a skewed tree), the program needs to visit all N nodes in the tree.
6+
7+
- The program processes each node exactly once and performs constant-time operations (addition and comparison) for each node.
8+
9+
- Therefore, the time complexity of this program is O(N), where N is the number of nodes in the binary tree.
10+
11+
**Space Complexity (Big O Space):**
12+
13+
The space complexity of the program is determined by the space required for the recursive function call stack. Here's the analysis of space complexity:
14+
15+
- The program uses a recursive function `findDepth` to traverse the binary tree. In the worst case, when the binary tree is completely unbalanced (e.g., a skewed tree), the maximum depth of the call stack can be equal to the height of the tree.
16+
17+
- For a balanced binary tree, the maximum depth of the call stack is proportional to the logarithm of the number of nodes (log₂(N)), which is the height of a balanced binary tree.
18+
19+
- Therefore, the space complexity due to the call stack is O(H), where H is the height of the binary tree.
20+
21+
- Additional space is used for the function's local variables, but this space is constant and does not depend on the input size.
22+
23+
- Overall, the space complexity of the program is O(H), where H is the height of the binary tree, and it represents the maximum depth of the call stack during the recursive traversal.
24+
25+
In summary, the time complexity of the program is O(N) because it visits each node once, and the space complexity is O(H), where H is the height of the binary tree. The space complexity is primarily determined by the maximum depth of the call stack, which can vary depending on the shape of the tree.

src.save/main/java/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package g0101_0200.s0105_construct_binary_tree_from_preorder_and_inorder_traversal;
22

33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Tree #Binary_Tree
4-
// #Divide_and_Conquer #Data_Structure_II_Day_15_Tree
4+
// #Divide_and_Conquer #Data_Structure_II_Day_15_Tree #Big_O_Time_O(N)_Space_O(N)
55
// #2022_06_22_Time_3_ms_(86.35%)_Space_45.2_MB_(14.09%)
66

77
import com_github_leetcode.TreeNode;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
**Time Complexity (Big O Time):**
2+
3+
The time complexity of the program can be analyzed as follows:
4+
5+
- The `buildTree` function initializes a map (`map`) and populates it with values from the `inorder` array. The population process takes O(N) time, where N is the number of nodes in the binary tree.
6+
7+
- The `answer` function is a recursive function that constructs the binary tree. It uses the `preorder` array to determine the root of the current subtree. For each subtree, it finds the index of the root value in the `inorder` array using the `map`. This operation takes O(1) time.
8+
9+
- In each recursive call, the program divides the problem into two subproblems: constructing the left subtree and constructing the right subtree. The division and recursive calls are performed for each node in the tree.
10+
11+
- Since each node is processed once in the recursive calls, and there are N nodes in the binary tree, the overall time complexity of the program is O(N).
12+
13+
**Space Complexity (Big O Space):**
14+
15+
The space complexity of the program is determined by the space required for data structures and the call stack. Here's the analysis of space complexity:
16+
17+
- The `map` data structure is used to store mappings between values and their indices in the `inorder` array. The space used by this map is proportional to the number of nodes in the binary tree, which is O(N).
18+
19+
- The recursive function `answer` utilizes the call stack to keep track of the recursive calls. In the worst case, when the binary tree is completely unbalanced (e.g., a skewed tree), the maximum depth of the call stack can be equal to the height of the tree, which is N in the worst case.
20+
21+
- Therefore, the space complexity due to the call stack is O(N) in the worst case.
22+
23+
- Additionally, there are a few integer variables and temporary variables used in the program. However, the space used by these variables is constant and does not depend on the input size.
24+
25+
- Overall, the space complexity of the program is O(N) due to the map and the call stack.
26+
27+
In summary, the time complexity of the program is O(N), and the space complexity is O(N) in the worst case. The space complexity is primarily determined by the map and the maximum depth of the call stack during the recursive construction of the binary tree.

src.save/main/java/g0101_0200/s0114_flatten_binary_tree_to_linked_list/Solution.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package g0101_0200.s0114_flatten_binary_tree_to_linked_list;
22

33
// #Medium #Top_100_Liked_Questions #Depth_First_Search #Tree #Binary_Tree #Stack #Linked_List
4-
// #Udemy_Linked_List #2022_06_23_Time_1_ms_(75.27%)_Space_42.8_MB_(36.48%)
4+
// #Udemy_Linked_List #Big_O_Time_O(N)_Space_O(N)
5+
// #2022_06_23_Time_1_ms_(75.27%)_Space_42.8_MB_(36.48%)
56

67
import com_github_leetcode.TreeNode;
78

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
**Time Complexity (Big O Time):**
2+
3+
The time complexity of the program can be analyzed as follows:
4+
5+
- The `flatten` function is called once with the root of the binary tree, and it, in turn, calls the `findTail` function recursively.
6+
7+
- The `findTail` function is a recursive function that traverses the left subtree of the current node and connects the right subtree to the left subtree to flatten the tree.
8+
9+
- In the worst case, the `findTail` function is called for each node in the binary tree exactly once. Each recursive call of `findTail` takes constant time O(1) for node manipulations.
10+
11+
- Therefore, the overall time complexity of the program is O(N), where N is the number of nodes in the binary tree.
12+
13+
**Space Complexity (Big O Space):**
14+
15+
The space complexity of the program is determined by the space required for the recursive call stack and the space for temporary variables. Here's the analysis of space complexity:
16+
17+
- The primary factor contributing to space complexity is the recursive call stack. In the worst case, when the binary tree is completely unbalanced (e.g., a skewed tree), the maximum depth of the call stack can be equal to the height of the tree, which is N in the worst case.
18+
19+
- Additionally, the program uses a few temporary variables like `left`, `right`, and `tail`, which occupy constant space.
20+
21+
- Therefore, the space complexity due to the call stack is O(N) in the worst case, and the space complexity due to temporary variables is constant O(1).
22+
23+
- Overall, the space complexity of the program is O(N) due to the call stack, which can grow linearly with the number of nodes in the binary tree.
24+
25+
In summary, the time complexity of the program is O(N), and the space complexity is O(N) in the worst case. The space complexity is primarily determined by the call stack depth during the recursive flattening of the binary tree.

0 commit comments

Comments
 (0)