Skip to content

Commit 13c4284

Browse files
authored
Added tasks 101-128
1 parent 5053e5c commit 13c4284

File tree

17 files changed

+857
-0
lines changed

17 files changed

+857
-0
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,13 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
104104

105105
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
106106
|-|-|-|-|-|-
107+
| 0121 |[Best Time to Buy and Sell Stock](src/main/python/g0101_0200/s0121_best_time_to_buy_and_sell_stock/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 666 | 98.09
107108

108109
#### Day 6 Tree
109110

110111
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
111112
|-|-|-|-|-|-
113+
| 0102 |[Binary Tree Level Order Traversal](src/main/python/g0101_0200/s0102_binary_tree_level_order_traversal/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 31 | 95.63
112114

113115
#### Day 7 Binary Search
114116

@@ -249,6 +251,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
249251

250252
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
251253
|-|-|-|-|-|-
254+
| 0101 |[Symmetric Tree](src/main/python/g0101_0200/s0101_symmetric_tree/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 35 | 71.46
252255

253256
#### Day 16 Design
254257

@@ -308,6 +311,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
308311

309312
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
310313
|-|-|-|-|-|-
314+
| 0121 |[Best Time to Buy and Sell Stock](src/main/python/g0101_0200/s0121_best_time_to_buy_and_sell_stock/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 666 | 98.09
311315
| 0283 |[Move Zeroes](src/main/python/g0201_0300/s0283_move_zeroes/Solution.py)| Easy | Top_100_Liked_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 113 | 97.70
312316
| 0001 |[Two Sum](src/main/python/g0001_0100/s0001_two_sum/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 62 | 53.52
313317
| 0055 |[Jump Game](src/main/python/g0001_0100/s0055_jump_game/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 333 | 97.58
@@ -347,6 +351,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
347351

348352
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
349353
|-|-|-|-|-|-
354+
| 0114 |[Flatten Binary Tree to Linked List](src/main/python/g0101_0200/s0114_flatten_binary_tree_to_linked_list/Solution.py)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Big_O_Time_O(N)_Space_O(N) | 24 | 99.54
350355
| 0024 |[Swap Nodes in Pairs](src/main/python/g0001_0100/s0024_swap_nodes_in_pairs/Solution.py)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 27 | 93.88
351356
| 0206 |[Reverse Linked List](src/main/python/g0201_0300/s0206_reverse_linked_list/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 35 | 72.56
352357
| 0021 |[Merge Two Sorted Lists](src/main/python/g0001_0100/s0021_merge_two_sorted_lists/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 31 | 92.02
@@ -358,8 +363,11 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
358363
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
359364
|-|-|-|-|-|-
360365
| 0094 |[Binary Tree Inorder Traversal](src/main/python/g0001_0100/s0094_binary_tree_inorder_traversal/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Big_O_Time_O(n)_Space_O(n) | 28 | 91.67
366+
| 0102 |[Binary Tree Level Order Traversal](src/main/python/g0101_0200/s0102_binary_tree_level_order_traversal/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 31 | 95.63
361367
| 0543 |[Diameter of Binary Tree](src/main/python/g0501_0600/s0543_diameter_of_binary_tree/Solution.py)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 51 | 36.84
362368
| 0226 |[Invert Binary Tree](src/main/python/g0201_0300/s0226_invert_binary_tree/Solution.py)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 30 | 87.85
369+
| 0104 |[Maximum Depth of Binary Tree](src/main/python/g0101_0200/s0104_maximum_depth_of_binary_tree/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(H) | 35 | 85.47
370+
| 0124 |[Binary Tree Maximum Path Sum](src/main/python/g0101_0200/s0124_binary_tree_maximum_path_sum/Solution.py)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 57 | 86.71
363371
| 0098 |[Validate Binary Search Tree](src/main/python/g0001_0100/s0098_validate_binary_search_tree/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 38 | 77.48
364372
| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/python/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/Solution.py)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 43 | 89.34
365373

@@ -424,6 +432,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
424432

425433
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
426434
|-|-|-|-|-|-
435+
| 0121 |[Best Time to Buy and Sell Stock](src/main/python/g0101_0200/s0121_best_time_to_buy_and_sell_stock/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 666 | 98.09
427436

428437
#### Day 4 Array
429438

@@ -469,6 +478,9 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
469478

470479
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
471480
|-|-|-|-|-|-
481+
| 0102 |[Binary Tree Level Order Traversal](src/main/python/g0101_0200/s0102_binary_tree_level_order_traversal/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 31 | 95.63
482+
| 0104 |[Maximum Depth of Binary Tree](src/main/python/g0101_0200/s0104_maximum_depth_of_binary_tree/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(H) | 35 | 85.47
483+
| 0101 |[Symmetric Tree](src/main/python/g0101_0200/s0101_symmetric_tree/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 35 | 71.46
472484

473485
#### Day 12 Tree
474486

@@ -576,6 +588,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
576588

577589
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
578590
|-|-|-|-|-|-
591+
| 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/python/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer, Big_O_Time_O(N)_Space_O(N) | 45 | 96.51
579592

580593
#### Day 16 Tree
581594

@@ -1028,6 +1041,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
10281041

10291042
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10301043
|-|-|-|-|-|-
1044+
| 0121 |[Best Time to Buy and Sell Stock](src/main/python/g0101_0200/s0121_best_time_to_buy_and_sell_stock/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 666 | 98.09
10311045

10321046
#### Day 8
10331047

@@ -1161,6 +1175,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
11611175

11621176
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11631177
|-|-|-|-|-|-
1178+
| 0104 |[Maximum Depth of Binary Tree](src/main/python/g0101_0200/s0104_maximum_depth_of_binary_tree/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(H) | 35 | 85.47
11641179

11651180
#### Day 11 Containers and Libraries
11661181

@@ -1385,6 +1400,14 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
13851400
| 0208 |[Implement Trie (Prefix Tree)](src/main/python/g0201_0300/s0208_implement_trie_prefix_tree/Trie.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Design, Trie, Level_2_Day_16_Design, Udemy_Trie_and_Heap, Big_O_Time_O(word.length())_or_O(prefix.length())_Space_O(N) | 140 | 28.87
13861401
| 0207 |[Course Schedule](src/main/python/g0201_0300/s0207_course_schedule/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort, Big_O_Time_O(N)_Space_O(N) | 86 | 76.64
13871402
| 0206 |[Reverse Linked List](src/main/python/g0201_0300/s0206_reverse_linked_list/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Data_Structure_I_Day_8_Linked_List, Algorithm_I_Day_10_Recursion_Backtracking, Level_1_Day_3_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 35 | 72.56
1403+
| 0128 |[Longest Consecutive Sequence](src/main/python/g0101_0200/s0128_longest_consecutive_sequence/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Union_Find, Big_O_Time_O(N_log_N)_Space_O(1) | 338 | 82.56
1404+
| 0124 |[Binary Tree Maximum Path Sum](src/main/python/g0101_0200/s0124_binary_tree_maximum_path_sum/Solution.py)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(N)_Space_O(N) | 57 | 86.71
1405+
| 0121 |[Best Time to Buy and Sell Stock](src/main/python/g0101_0200/s0121_best_time_to_buy_and_sell_stock/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Data_Structure_I_Day_3_Array, Dynamic_Programming_I_Day_7, Level_1_Day_5_Greedy, Udemy_Arrays, Big_O_Time_O(N)_Space_O(1) | 666 | 98.09
1406+
| 0114 |[Flatten Binary Tree to Linked List](src/main/python/g0101_0200/s0114_flatten_binary_tree_to_linked_list/Solution.py)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(N) | 24 | 99.54
1407+
| 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/python/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer, Data_Structure_II_Day_15_Tree, Big_O_Time_O(N)_Space_O(N) | 45 | 96.51
1408+
| 0104 |[Maximum Depth of Binary Tree](src/main/python/g0101_0200/s0104_maximum_depth_of_binary_tree/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Programming_Skills_I_Day_10_Linked_List_and_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(N)_Space_O(H) | 35 | 85.47
1409+
| 0102 |[Binary Tree Level Order Traversal](src/main/python/g0101_0200/s0102_binary_tree_level_order_traversal/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Level_1_Day_6_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(N)_Space_O(N) | 31 | 95.63
1410+
| 0101 |[Symmetric Tree](src/main/python/g0101_0200/s0101_symmetric_tree/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Level_2_Day_15_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 35 | 71.46
13881411
| 0098 |[Validate Binary Search Tree](src/main/python/g0001_0100/s0098_validate_binary_search_tree/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_I_Day_14_Tree, Level_1_Day_8_Binary_Search_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(N)_Space_O(log(N)) | 38 | 77.48
13891412
| 0096 |[Unique Binary Search Trees](src/main/python/g0001_0100/s0096_unique_binary_search_trees/Solution.py)| Medium | Dynamic_Programming, Math, Tree, Binary_Tree, Binary_Search_Tree, Dynamic_Programming_I_Day_11, Big_O_Time_O(n)_Space_O(1) | 30 | 84.13
13901413
| 0094 |[Binary Tree Inorder Traversal](src/main/python/g0001_0100/s0094_binary_tree_inorder_traversal/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Data_Structure_I_Day_10_Tree, Udemy_Tree_Stack_Queue, Big_O_Time_O(n)_Space_O(n) | 28 | 91.67
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Breadth_First_Search
2+
# #Tree #Binary_Tree #Data_Structure_I_Day_11_Tree #Level_2_Day_15_Tree
3+
# #Big_O_Time_O(N)_Space_O(log(N)) #2024_06_09_Time_35_ms_(71.46%)_Space_16.6_MB_(14.05%)
4+
5+
# Definition for a binary tree node.
6+
# class TreeNode:
7+
# def __init__(self, val=0, left=None, right=None):
8+
# self.val = val
9+
# self.left = left
10+
# self.right = right
11+
class Solution:
12+
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
13+
if root is None:
14+
return True
15+
return self.helper(root.left, root.right)
16+
17+
def helper(self, leftNode: TreeNode, rightNode: TreeNode) -> bool:
18+
if leftNode is None or rightNode is None:
19+
return leftNode is None and rightNode is None
20+
if leftNode.val != rightNode.val:
21+
return False
22+
return self.helper(leftNode.left, rightNode.right) and self.helper(leftNode.right, rightNode.left)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
101\. Symmetric Tree
2+
3+
Easy
4+
5+
Given the `root` of a binary tree, _check whether it is a mirror of itself_ (i.e., symmetric around its center).
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg)
10+
11+
**Input:** root = [1,2,2,3,4,4,3]
12+
13+
**Output:** true
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg)
18+
19+
**Input:** root = [1,2,2,null,3,null,3]
20+
21+
**Output:** false
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the tree is in the range `[1, 1000]`.
26+
* `-100 <= Node.val <= 100`
27+
28+
**Follow up:** Could you solve it both recursively and iteratively?
29+
30+
To solve the "Symmetric Tree" problem in Python with the Solution class, follow these steps:
31+
32+
1. Define a method `isSymmetric` in the `Solution` class that takes the root of a binary tree as input and returns true if the tree is symmetric, and false otherwise.
33+
2. Implement a recursive approach to check if the given binary tree is symmetric:
34+
- Define a helper method `isMirror` that takes two tree nodes as input parameters.
35+
- In the `isMirror` method, recursively compare the left and right subtrees of the given nodes.
36+
- At each step, check if the values of the corresponding nodes are equal and if the left subtree of one node is a mirror image of the right subtree of the other node.
37+
- If both conditions are satisfied for all corresponding nodes, return true; otherwise, return false.
38+
3. Call the `isMirror` method with the root's left and right children to check if the entire tree is symmetric.
39+
40+
Here's the implementation of the `isSymmetric` method in Python:
41+
42+
```python
43+
# Definition for a binary tree node.
44+
# class TreeNode:
45+
# def __init__(self, val=0, left=None, right=None):
46+
# self.val = val
47+
# self.left = left
48+
# self.right = right
49+
class Solution:
50+
def isSymmetric(self, root: TreeNode) -> bool:
51+
if root is None:
52+
return True
53+
return self.helper(root.left, root.right)
54+
55+
def helper(self, leftNode: TreeNode, rightNode: TreeNode) -> bool:
56+
if leftNode is None or rightNode is None:
57+
return leftNode is None and rightNode is None
58+
if leftNode.val != rightNode.val:
59+
return False
60+
return self.helper(leftNode.left, rightNode.right) and self.helper(leftNode.right, rightNode.left)
61+
```
62+
63+
This implementation recursively checks whether the given binary tree is symmetric around its center in O(n) time complexity, where n is the number of nodes in the tree.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Breadth_First_Search #Tree
2+
# #Binary_Tree #Data_Structure_I_Day_11_Tree #Level_1_Day_6_Tree #Udemy_Tree_Stack_Queue
3+
# #Big_O_Time_O(N)_Space_O(N) #2024_06_09_Time_31_ms_(95.63%)_Space_17.2_MB_(85.23%)
4+
5+
# Definition for a binary tree node.
6+
# class TreeNode:
7+
# def __init__(self, val=0, left=None, right=None):
8+
# self.val = val
9+
# self.left = left
10+
# self.right = right
11+
12+
from collections import deque
13+
14+
class Solution:
15+
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
16+
result = []
17+
if not root:
18+
return result
19+
20+
queue = deque()
21+
queue.append(root)
22+
queue.append(None)
23+
24+
level = []
25+
while queue:
26+
node = queue.popleft()
27+
if node is not None:
28+
level.append(node.val)
29+
if node.left:
30+
queue.append(node.left)
31+
if node.right:
32+
queue.append(node.right)
33+
else:
34+
result.append(level)
35+
level = []
36+
if queue:
37+
queue.append(None)
38+
39+
return result

0 commit comments

Comments
 (0)