mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-19 12:24:34 +08:00
Add typing annotations to Python codes. (#411)
This commit is contained in:
@ -5,36 +5,26 @@ Author: Krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
import collections
|
||||
from typing import List, Deque, Optional
|
||||
|
||||
class TreeNode:
|
||||
"""Definition for a binary tree node
|
||||
"""
|
||||
def __init__(self, val=0, left=None, right=None):
|
||||
self.val = val # 结点值
|
||||
self.height = 0 # 结点高度
|
||||
self.left = left # 左子结点引用
|
||||
self.right = right # 右子结点引用
|
||||
""" Definition for a binary tree node """
|
||||
def __init__(self, val: int = 0, left: Optional['TreeNode'] = None, right: Optional['TreeNode'] = None):
|
||||
self.val: int = val # 结点值
|
||||
self.height: int = 0 # 结点高度
|
||||
self.left: Optional[TreeNode] = left # 左子结点引用
|
||||
self.right: Optional[TreeNode] = right # 右子结点引用
|
||||
|
||||
def __str__(self):
|
||||
val = self.val
|
||||
left_node_val = self.left.val if self.left else None
|
||||
right_node_val = self.right.val if self.right else None
|
||||
return "<TreeNode: {}, leftTreeNode: {}, rightTreeNode: {}>".format(val, left_node_val, right_node_val)
|
||||
|
||||
__repr__ = __str__
|
||||
|
||||
|
||||
def list_to_tree(arr):
|
||||
"""Generate a binary tree with a list
|
||||
"""
|
||||
def list_to_tree(arr: List[int]) -> Optional[TreeNode]:
|
||||
""" Generate a binary tree with a list """
|
||||
if not arr:
|
||||
return None
|
||||
|
||||
i = 0
|
||||
i: int = 0
|
||||
root = TreeNode(arr[0])
|
||||
queue = collections.deque([root])
|
||||
queue: Deque[TreeNode] = collections.deque([root])
|
||||
while queue:
|
||||
node = queue.popleft()
|
||||
node: TreeNode = queue.popleft()
|
||||
i += 1
|
||||
if i >= len(arr): break
|
||||
if arr[i] != None:
|
||||
@ -48,15 +38,14 @@ def list_to_tree(arr):
|
||||
|
||||
return root
|
||||
|
||||
def tree_to_list(root):
|
||||
"""Serialize a tree into an array
|
||||
"""
|
||||
def tree_to_list(root: Optional[TreeNode]) -> List[int]:
|
||||
""" Serialize a tree into an array """
|
||||
if not root: return []
|
||||
queue = collections.deque()
|
||||
queue: Deque[TreeNode] = collections.deque()
|
||||
queue.append(root)
|
||||
res = []
|
||||
res: List[int] = []
|
||||
while queue:
|
||||
node = queue.popleft()
|
||||
node: Optional[TreeNode] = queue.popleft()
|
||||
if node:
|
||||
res.append(node.val)
|
||||
queue.append(node.left)
|
||||
@ -64,13 +53,12 @@ def tree_to_list(root):
|
||||
else: res.append(None)
|
||||
return res
|
||||
|
||||
def get_tree_node(root, val):
|
||||
"""Get a tree node with specific value in a binary tree
|
||||
"""
|
||||
def get_tree_node(root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
|
||||
""" Get a tree node with specific value in a binary tree """
|
||||
if not root:
|
||||
return
|
||||
if root.val == val:
|
||||
return root
|
||||
left = get_tree_node(root.left, val)
|
||||
right = get_tree_node(root.right, val)
|
||||
left: Optional[TreeNode] = get_tree_node(root.left, val)
|
||||
right: Optional[TreeNode] = get_tree_node(root.right, val)
|
||||
return left if left else right
|
||||
|
Reference in New Issue
Block a user