Trying to understand binary tree from array
This commit is contained in:
+53
-13
@@ -163,7 +163,7 @@ class MinStack:
|
||||
return self._min[-1]
|
||||
|
||||
|
||||
def linked_list_to_list(head: stdlib.ListNode | None) -> list[int]:
|
||||
def linked_list_to_list(head: stdlib.LinkedListNode | None) -> list[int]:
|
||||
seen: set[int] = set()
|
||||
ret: list[int] = []
|
||||
|
||||
@@ -178,9 +178,9 @@ def linked_list_to_list(head: stdlib.ListNode | None) -> list[int]:
|
||||
return ret
|
||||
|
||||
|
||||
def sort_linked_list(head: stdlib.ListNode | None) -> stdlib.ListNode | None:
|
||||
by_val: list[tuple[int, stdlib.ListNode]] = []
|
||||
ret: stdlib.ListNode | None = None
|
||||
def sort_linked_list(head: stdlib.LinkedListNode | None) -> stdlib.LinkedListNode | None:
|
||||
by_val: list[tuple[int, stdlib.LinkedListNode]] = []
|
||||
ret: stdlib.LinkedListNode | None = None
|
||||
|
||||
while head is not None:
|
||||
by_val.append((head.val, head))
|
||||
@@ -203,12 +203,13 @@ def sort_linked_list(head: stdlib.ListNode | None) -> stdlib.ListNode | None:
|
||||
|
||||
|
||||
def connect_binary_tree_right(
|
||||
root: stdlib.Node | None,
|
||||
) -> tuple[stdlib.Node | None, list[int | None]]:
|
||||
root: stdlib.ConnectableBinaryTreeNode | None,
|
||||
) -> tuple[stdlib.ConnectableBinaryTreeNode | None, list[int | None]]:
|
||||
if root is None:
|
||||
return None, []
|
||||
|
||||
by_level = binary_tree_by_level(copy.deepcopy(root))
|
||||
by_level = typing.cast(dict[int, list[stdlib.ConnectableBinaryTreeNode]], by_level)
|
||||
serialized: list[int | None] = []
|
||||
|
||||
print("")
|
||||
@@ -231,8 +232,10 @@ def connect_binary_tree_right(
|
||||
return connected_root, serialized
|
||||
|
||||
|
||||
def binary_tree_by_level(root: stdlib.Node) -> dict[int, list[stdlib.Node]]:
|
||||
combined: dict[int, list[stdlib.Node]] = {}
|
||||
def binary_tree_by_level(
|
||||
root: stdlib.BinaryTreeNode,
|
||||
) -> dict[int, list[stdlib.BinaryTreeNode]]:
|
||||
combined: dict[int, list[stdlib.BinaryTreeNode]] = {}
|
||||
|
||||
for path in collect_binary_tree_levels(0, root):
|
||||
level, node = path
|
||||
@@ -243,8 +246,8 @@ def binary_tree_by_level(root: stdlib.Node) -> dict[int, list[stdlib.Node]]:
|
||||
|
||||
|
||||
def collect_binary_tree_levels(
|
||||
level: int, node: stdlib.Node | None
|
||||
) -> typing.Iterator[tuple[int, stdlib.Node]]:
|
||||
level: int, node: stdlib.BinaryTreeNode | None
|
||||
) -> typing.Iterator[tuple[int, stdlib.BinaryTreeNode]]:
|
||||
if node is None:
|
||||
return
|
||||
|
||||
@@ -253,7 +256,7 @@ def collect_binary_tree_levels(
|
||||
yield from collect_binary_tree_levels(level + 1, node.left)
|
||||
|
||||
|
||||
def sum_binary_tree_path_ints(root: stdlib.Node | None) -> int:
|
||||
def sum_binary_tree_path_ints(root: stdlib.BinaryTreeNode | None) -> int:
|
||||
path_ints: list[int] = []
|
||||
|
||||
for path in collect_binary_tree_paths(root):
|
||||
@@ -262,9 +265,20 @@ def sum_binary_tree_path_ints(root: stdlib.Node | None) -> int:
|
||||
return sum(path_ints)
|
||||
|
||||
|
||||
def binary_tree_paths_as_lists(
|
||||
paths: list[list[stdlib.BinaryTreeNode]],
|
||||
) -> list[list[int]]:
|
||||
paths_vals: list[list[int]] = []
|
||||
|
||||
for path in paths:
|
||||
paths_vals.append([node.val for node in path])
|
||||
|
||||
return paths_vals
|
||||
|
||||
|
||||
def collect_binary_tree_paths(
|
||||
node: stdlib.Node | None,
|
||||
) -> typing.Iterator[list[stdlib.Node]]:
|
||||
node: stdlib.BinaryTreeNode | None,
|
||||
) -> typing.Iterator[list[stdlib.BinaryTreeNode]]:
|
||||
if node is None:
|
||||
return
|
||||
|
||||
@@ -279,3 +293,29 @@ def collect_binary_tree_paths(
|
||||
if node.left is not None:
|
||||
for path in collect_binary_tree_paths(node.left):
|
||||
yield [node] + path
|
||||
|
||||
|
||||
def binary_tree_from_list(inlist: list[int | None]) -> stdlib.BinaryTreeNode | None:
|
||||
if len(inlist) == 0:
|
||||
return None
|
||||
|
||||
nodes: list[stdlib.BinaryTreeNode | None] = [
|
||||
typing.cast(stdlib.BinaryTreeNode | None, stdlib.TreeNode.from_int(i))
|
||||
for i in inlist
|
||||
]
|
||||
nodes_copy = nodes[::-1]
|
||||
root = nodes_copy.pop()
|
||||
|
||||
for node in nodes:
|
||||
if node is None:
|
||||
continue
|
||||
|
||||
if len(nodes_copy) == 0:
|
||||
break
|
||||
|
||||
node.left = nodes_copy.pop()
|
||||
|
||||
if len(nodes_copy) > 0:
|
||||
node.right = nodes_copy.pop()
|
||||
|
||||
return root
|
||||
|
||||
Reference in New Issue
Block a user