2022-12-23 00:44:40 +08:00
|
|
|
"""
|
2022-11-25 02:04:38 +08:00
|
|
|
File: binary_tree.py
|
|
|
|
Created Time: 2021-12-11
|
|
|
|
Author: Krahets (krahets@163.com)
|
2022-12-23 00:44:40 +08:00
|
|
|
"""
|
2022-11-25 02:04:38 +08:00
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
from collections import deque
|
2022-11-25 02:04:38 +08:00
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
2022-11-25 02:04:38 +08:00
|
|
|
class TreeNode:
|
2023-04-09 05:05:35 +08:00
|
|
|
"""Definition for a binary tree node"""
|
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
def __init__(self, val: int = 0):
|
2023-04-09 05:05:35 +08:00
|
|
|
self.val: int = val # 节点值
|
|
|
|
self.height: int = 0 # 节点高度
|
2023-04-09 04:32:17 +08:00
|
|
|
self.left: TreeNode | None = None # 左子节点引用
|
2023-04-09 05:05:35 +08:00
|
|
|
self.right: TreeNode | None = None # 右子节点引用
|
|
|
|
|
2022-12-27 18:34:12 +08:00
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
def list_to_tree(arr: list[int]) -> TreeNode | None:
|
2023-04-09 05:05:35 +08:00
|
|
|
"""Generate a binary tree with a list"""
|
2022-11-25 02:04:38 +08:00
|
|
|
if not arr:
|
2022-12-02 00:53:19 +08:00
|
|
|
return None
|
2023-04-09 05:05:35 +08:00
|
|
|
|
2023-05-22 22:03:57 +08:00
|
|
|
i = 0
|
2023-01-08 19:03:22 +08:00
|
|
|
root = TreeNode(arr[0])
|
2023-03-23 18:51:56 +08:00
|
|
|
queue: deque[TreeNode] = deque([root])
|
2022-11-25 02:04:38 +08:00
|
|
|
while queue:
|
2023-03-12 18:49:52 +08:00
|
|
|
node: TreeNode = queue.popleft()
|
2023-01-08 19:03:22 +08:00
|
|
|
i += 1
|
2023-04-09 05:05:35 +08:00
|
|
|
if i >= len(arr):
|
|
|
|
break
|
2022-11-25 02:04:38 +08:00
|
|
|
if arr[i] != None:
|
2023-01-08 19:03:22 +08:00
|
|
|
node.left = TreeNode(arr[i])
|
2022-11-25 02:04:38 +08:00
|
|
|
queue.append(node.left)
|
|
|
|
i += 1
|
2023-04-09 05:05:35 +08:00
|
|
|
if i >= len(arr):
|
|
|
|
break
|
2022-11-25 02:04:38 +08:00
|
|
|
if arr[i] != None:
|
2023-01-08 19:03:22 +08:00
|
|
|
node.right = TreeNode(arr[i])
|
2022-11-25 02:04:38 +08:00
|
|
|
queue.append(node.right)
|
2023-04-09 05:05:35 +08:00
|
|
|
|
2022-11-25 02:04:38 +08:00
|
|
|
return root
|
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
def tree_to_list(root: TreeNode | None) -> list[int]:
|
2023-04-09 05:05:35 +08:00
|
|
|
"""Serialize a tree into an array"""
|
|
|
|
if not root:
|
|
|
|
return []
|
2023-03-23 18:51:56 +08:00
|
|
|
queue: deque[TreeNode] = deque()
|
2022-11-25 02:04:38 +08:00
|
|
|
queue.append(root)
|
2023-03-23 18:51:56 +08:00
|
|
|
res: list[int] = []
|
2022-11-25 02:04:38 +08:00
|
|
|
while queue:
|
2023-03-23 18:51:56 +08:00
|
|
|
node: TreeNode | None = queue.popleft()
|
2022-11-25 02:04:38 +08:00
|
|
|
if node:
|
|
|
|
res.append(node.val)
|
|
|
|
queue.append(node.left)
|
|
|
|
queue.append(node.right)
|
2023-04-09 05:05:35 +08:00
|
|
|
else:
|
|
|
|
res.append(None)
|
2022-11-25 02:04:38 +08:00
|
|
|
return res
|
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
def get_tree_node(root: TreeNode | None, val: int) -> TreeNode | None:
|
2023-04-09 05:05:35 +08:00
|
|
|
"""Get a tree node with specific value in a binary tree"""
|
2022-11-25 02:04:38 +08:00
|
|
|
if not root:
|
|
|
|
return
|
|
|
|
if root.val == val:
|
|
|
|
return root
|
2023-03-23 18:51:56 +08:00
|
|
|
left: TreeNode | None = get_tree_node(root.left, val)
|
|
|
|
right: TreeNode | None = get_tree_node(root.right, val)
|
2022-11-25 02:04:38 +08:00
|
|
|
return left if left else right
|