Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Commit bb72bab

Browse files
add some fun tree playing
1 parent 48720ad commit bb72bab

6 files changed

+209
-0
lines changed
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def max_depth(root: Optional[TreeNode]) -> int:
2+
3+
if root is None:
4+
return 0
5+
6+
return max(max_depth(root.left) + 1, max_depth(root.right) + 1)

trees_and_graphs/has_path_sum.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def has_path_sum(root: Optional[TreeNode], target_sum: int) -> bool:
2+
3+
def transverse(node, sum_here=0):
4+
5+
if not node:
6+
return sum_here == target_sum
7+
8+
sum_here += node.val
9+
10+
if not node.left:
11+
return transverse(node.right, sum_here)
12+
if not node.right:
13+
return transverse(node.left, sum_here)
14+
else:
15+
return transverse(node.left, sum_here) or transverse(node.right, sum_here)
16+
17+
if not root:
18+
return False
19+
20+
return transverse(root)
21+

trees_and_graphs/is_tree_symmetric.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
def is_symmetrical(root: Optional[TreeNode]) -> bool:
2+
3+
stack = [(root, root)]
4+
5+
while stack:
6+
7+
node1, node2 = stack.pop()
8+
9+
if (not node1 and node2) or (not node2 and node1):
10+
return False
11+
12+
elif not node1 and not node2:
13+
continue
14+
15+
elif node1 and node2 and node1.val != node2.val:
16+
return False
17+
18+
stack.append([node1.left, node2.right])
19+
stack.append([node1.right, node2.left])
20+
21+
return True
22+
23+
24+
def is_symmetrical_recursive(root: Optional[TreeNode]) -> bool:
25+
26+
def helper(node1, node2):
27+
if (not node1 and node2) or \
28+
(not node2 and node1) or \
29+
(node1 and node2 and node1.val != node2.val):
30+
return False
31+
32+
if (not node1 and not node2):
33+
return True
34+
35+
return helper(node1.left, node2.right) and helper(node2.left, node1.right)
36+
37+
return helper(root.left, root.right)
38+
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# recursive and iterative inorder traversal
2+
3+
def preorder_recursive(root: Optional[TreeNode]) -> list[int]:
4+
5+
if root == None:
6+
return []
7+
8+
return [root.val] + preorder_recursive(root.left) + preorder_recursive(root.right)
9+
10+
11+
def preorder_iterative(root: Optional[TreeNode]) -> list[int]:
12+
13+
result = []
14+
stack = [root]
15+
16+
while stack:
17+
18+
current = stack.pop()
19+
result.append(current.val)
20+
21+
if current.right:
22+
stack.append(current.right)
23+
24+
if current.left:
25+
stack.append(current.left)
26+
27+
return result
28+
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# given a collection of numbers, find the pair
2+
# of numbers that sum to a given number
3+
4+
5+
6+
def bs(array, desired_num):
7+
8+
start = 0
9+
end = len(array)
10+
mid = (end - start) // 2
11+
12+
while len(array) > 0:
13+
if array[mid] == desired_num:
14+
return True
15+
elif array[mid] > desired_num:
16+
return bs(array[mid+1:], desired_num)
17+
else:
18+
return bs(array[:mid], desired_num)
19+
20+
return False
21+
22+
23+
def find_pairs_bs(array, desired_sum):
24+
25+
for i in range(len(array)):
26+
num1 = array[i]
27+
desired_num = desired_sum - num1
28+
if bs(array[i + 1:], desired_num) == True:
29+
return (num1, desired_num)
30+
31+
return False
32+
33+
34+
def find_pairs_max_sum(array, desired_sum):
35+
36+
i, j = 0, len(array) - 1
37+
38+
while i < j:
39+
if array[i] + array[j] == desired_sum:
40+
return array[i], array[j]
41+
elif array[i] + array[j] > desired_sum:
42+
j = j - 1
43+
elif array[i] + array[j] < desired_sum:
44+
i = i + 1
45+
46+
return False
47+
48+
def find_pairs_not_sorted(array, desired_sum):
49+
50+
lookup = {}
51+
52+
for item in array:
53+
key = desired_sum - item
54+
55+
if key in lookup.keys():
56+
lookup[key] += 1
57+
else:
58+
lookup[key] = 1
59+
60+
for item in array:
61+
key = desired_sum - item
62+
63+
if item in lookup.keys():
64+
if lookup[item] == 1:
65+
return (item, key)
66+
else:
67+
lookup[item] -= 1
68+
69+
return False
70+
71+
72+
73+
if __name__ == "__main__":
74+
75+
desired_sum = 8
76+
array1 = [1, 2, 3, 9]
77+
array2 = [1, 2, 4, 5, 4]
78+
array3 = [2, 1, 6, 3, 11, 2]
79+
80+
assert(find_pairs_bs(array1, desired_sum) == False)
81+
assert(find_pairs_bs(array2, desired_sum) == (4, 4))
82+
assert(find_pairs_max_sum(array1, desired_sum) == False)
83+
assert(find_pairs_max_sum(array2, desired_sum) == (4,4))
84+
assert(find_pairs_not_sorted(array1, desired_sum) == False)
85+
assert(find_pairs_not_sorted(array2, desired_sum) == (4, 4))
86+
assert(find_pairs_not_sorted(array3, desired_sum) == (2, 6))
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Given the root of a binary tree, return the level order traversal of its nodes' values.
2+
# (i.e., from left to right, level by level).
3+
4+
5+
def levelOrder(root: Optional[TreeNode]) -> list[list[int]]:
6+
7+
if root is None:
8+
return []
9+
10+
queue = collections.deque()
11+
queue.append(root)
12+
result = []
13+
14+
while queue:
15+
16+
this_level = []
17+
18+
for _ in range(len(queue)):
19+
20+
current = queue.popleft()
21+
22+
if current:
23+
this_level.append(current.val)
24+
queue.append(current.left)
25+
queue.append(current.right)
26+
27+
if this_level:
28+
result.append(this_level)
29+
30+
return result

0 commit comments

Comments
 (0)