Skip to content

Commit 45a632b

Browse files
authored
Added tests 104-139
1 parent c094347 commit 45a632b

File tree

20 files changed

+316
-0
lines changed

20 files changed

+316
-0
lines changed

src/main/python/g0101_0200/s0104_maximum_depth_of_binary_tree/Solution0104.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
# #Top_Interview_150_Binary_Tree_General #Big_O_Time_O(N)_Space_O(H)
55
# #2025_07_25_Time_0_ms_(100.00%)_Space_19.23_MB_(7.43%)
66

7+
from typing import Optional
8+
class TreeNode:
9+
def __init__(self, val=0, left=None, right=None):
10+
self.val = val
11+
self.left = left
12+
self.right = right
13+
714
# Definition for a binary tree node.
815
# class TreeNode:
916
# def __init__(self, val=0, left=None, right=None):
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import unittest
2+
from Solution0104 import Solution, TreeNode
3+
4+
def build_tree(values):
5+
if not values:
6+
return None
7+
root = TreeNode(values[0])
8+
queue = [root]
9+
i = 1
10+
while queue and i < len(values):
11+
node = queue.pop(0)
12+
if i < len(values) and values[i] is not None:
13+
node.left = TreeNode(values[i])
14+
queue.append(node.left)
15+
i += 1
16+
if i < len(values) and values[i] is not None:
17+
node.right = TreeNode(values[i])
18+
queue.append(node.right)
19+
i += 1
20+
return root
21+
22+
class SolutionTest(unittest.TestCase):
23+
def test_maxDepth(self):
24+
root = build_tree([3, 9, 20, None, None, 15, 7])
25+
self.assertEqual(Solution().maxDepth(root), 3)
26+
27+
def test_maxDepth2(self):
28+
root = build_tree([1, None, 2])
29+
self.assertEqual(Solution().maxDepth(root), 2)
30+
31+
def test_maxDepth3(self):
32+
root = build_tree([])
33+
self.assertEqual(Solution().maxDepth(root), 0)

src/main/python/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/Solution0105.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
# #Divide_and_Conquer #Data_Structure_II_Day_15_Tree #Top_Interview_150_Binary_Tree_General
33
# #Big_O_Time_O(N)_Space_O(N) #2025_07_25_Time_4_ms_(73.84%)_Space_19.14_MB_(84.59%)
44

5+
from typing import Optional, List
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+
512
# Definition for a binary tree node.
613
# class TreeNode:
714
# def __init__(self, val=0, left=None, right=None):
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import unittest
2+
from Solution0105 import Solution, TreeNode
3+
4+
def tree_to_list(root):
5+
if not root:
6+
return []
7+
result = []
8+
queue = [root]
9+
while queue:
10+
node = queue.pop(0)
11+
if node:
12+
result.append(node.val)
13+
queue.append(node.left)
14+
queue.append(node.right)
15+
else:
16+
result.append(None)
17+
# Remove trailing None values
18+
while result and result[-1] is None:
19+
result.pop()
20+
return result
21+
22+
class SolutionTest(unittest.TestCase):
23+
def test_buildTree(self):
24+
preorder = [3, 9, 20, 15, 7]
25+
inorder = [9, 3, 15, 20, 7]
26+
result = Solution().buildTree(preorder, inorder)
27+
# Check that the tree structure is correct by comparing values
28+
self.assertEqual(result.val, 3)
29+
self.assertEqual(result.left.val, 9)
30+
self.assertEqual(result.right.val, 20)
31+
32+
def test_buildTree2(self):
33+
preorder = [-1]
34+
inorder = [-1]
35+
result = Solution().buildTree(preorder, inorder)
36+
self.assertEqual(result.val, -1)
37+
self.assertIsNone(result.left)
38+
self.assertIsNone(result.right)

src/main/python/g0101_0200/s0114_flatten_binary_tree_to_linked_list/Solution0114.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
# #Udemy_Linked_List #Top_Interview_150_Binary_Tree_General #Big_O_Time_O(N)_Space_O(N)
33
# #2025_07_25_Time_0_ms_(100.00%)_Space_17.94_MB_(73.46%)
44

5+
from typing import Optional
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+
512
# Definition for a binary tree node.
613
# class TreeNode:
714
# def __init__(self, val=0, left=None, right=None):
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import unittest
2+
from Solution0114 import Solution, TreeNode
3+
4+
def build_tree(values):
5+
if not values:
6+
return None
7+
root = TreeNode(values[0])
8+
queue = [root]
9+
i = 1
10+
while queue and i < len(values):
11+
node = queue.pop(0)
12+
if i < len(values) and values[i] is not None:
13+
node.left = TreeNode(values[i])
14+
queue.append(node.left)
15+
i += 1
16+
if i < len(values) and values[i] is not None:
17+
node.right = TreeNode(values[i])
18+
queue.append(node.right)
19+
i += 1
20+
return root
21+
22+
def flatten_to_list(root):
23+
result = []
24+
current = root
25+
while current:
26+
result.append(current.val)
27+
current = current.right
28+
return result
29+
30+
31+
class SolutionTest(unittest.TestCase):
32+
def test_flatten(self):
33+
root = build_tree([1, 2, 5, 3, 4, None, 6])
34+
Solution().flatten(root)
35+
expected = [1, 2, 3, 4, 5, 6]
36+
self.assertEqual(flatten_to_list(root), expected)
37+
38+
def test_flatten2(self):
39+
root = build_tree([])
40+
Solution().flatten(root)
41+
self.assertIsNone(root)
42+
43+
def test_flatten3(self):
44+
root = build_tree([0])
45+
Solution().flatten(root)
46+
expected = [0]
47+
self.assertEqual(flatten_to_list(root), expected)

src/main/python/g0101_0200/s0121_best_time_to_buy_and_sell_stock/Solution0121.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# #Top_Interview_150_Array/String #Big_O_Time_O(N)_Space_O(1)
44
# #2025_07_25_Time_17_ms_(98.03%)_Space_26.89_MB_(77.51%)
55

6+
from typing import List
7+
68
class Solution:
79
def maxProfit(self, prices: List[int]) -> int:
810
min_price = float('inf')
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import unittest
2+
from Solution0121 import Solution
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_maxProfit(self):
6+
self.assertEqual(Solution().maxProfit([7,1,5,3,6,4]), 5)
7+
8+
def test_maxProfit2(self):
9+
self.assertEqual(Solution().maxProfit([7,6,4,3,1]), 0)
10+
11+
def test_maxProfit3(self):
12+
self.assertEqual(Solution().maxProfit([1]), 0)

src/main/python/g0101_0200/s0124_binary_tree_maximum_path_sum/Solution0124.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
# #Tree #Binary_Tree #Udemy_Tree_Stack_Queue #Top_Interview_150_Binary_Tree_General
33
# #Big_O_Time_O(N)_Space_O(N) #2025_07_25_Time_11_ms_(91.40%)_Space_22.92_MB_(80.36%)
44

5+
from typing import Optional
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+
512
# Definition for a binary tree node.
613
# class TreeNode:
714
# def __init__(self, val=0, left=None, right=None):
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import unittest
2+
from Solution0124 import Solution, TreeNode
3+
4+
def build_tree(values):
5+
if not values:
6+
return None
7+
root = TreeNode(values[0])
8+
queue = [root]
9+
i = 1
10+
while queue and i < len(values):
11+
node = queue.pop(0)
12+
if i < len(values) and values[i] is not None:
13+
node.left = TreeNode(values[i])
14+
queue.append(node.left)
15+
i += 1
16+
if i < len(values) and values[i] is not None:
17+
node.right = TreeNode(values[i])
18+
queue.append(node.right)
19+
i += 1
20+
return root
21+
22+
class SolutionTest(unittest.TestCase):
23+
def test_maxPathSum(self):
24+
root = build_tree([1, 2, 3])
25+
self.assertEqual(Solution().maxPathSum(root), 6)
26+
27+
def test_maxPathSum2(self):
28+
root = build_tree([-10, 9, 20, None, None, 15, 7])
29+
self.assertEqual(Solution().maxPathSum(root), 42)
30+
31+
def test_maxPathSum3(self):
32+
root = build_tree([-3])
33+
self.assertEqual(Solution().maxPathSum(root), -3)

0 commit comments

Comments
 (0)