一:樹
1、樹描述的是一個父子關係;有節點;根節點;葉子節點三個相關的概念
2、樹的高度;深度;層
3、二叉樹:每個節點最多隻有兩個孩子
4、完全二叉樹:除了葉子節點;每個孩子並不要求都為兩個孩子(從上到下,從左到右依次填滿節點)
5、滿二叉樹:除了葉子節點;每個節點都有兩個孩子
6、二叉樹的遍歷
(1)前序遍歷:根節點-->左孩子-->右孩子
(2)中序遍歷:左孩子-->根節點-->右孩子
(3)後序遍歷:左孩子-->右孩子-->根節點
7、樹在程式設計當中;一般會直接給出樹的結構;不會讓你去建立一個樹
二:刷題
144 後序遍歷二叉樹
(1)思路就是先將陣列轉化二二叉樹;然後根據後序遍歷的特性依次訪問左子樹;右子樹;最後訪問根節點
from collections import deque
# 定義二叉樹節點的類
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
# 迭代方法將列表轉換為二叉樹
def build_tree_iterative(nodes):
if not nodes:
return None
root = TreeNode(nodes[0]) # 建立根節點
queue = deque([root]) # 使用佇列來追蹤節點
index = 1
while queue and index < len(nodes):
node = queue.popleft() # 取出佇列中的第一個節點
# 處理左子樹
if index < len(nodes) and nodes[index] is not None:
node.left = TreeNode(nodes[index])
queue.append(node.left) # 左子節點加入佇列
index += 1
# 處理右子樹
if index < len(nodes) and nodes[index] is not None:
node.right = TreeNode(nodes[index])
queue.append(node.right) # 右子節點加入佇列
index += 1
return root
# 層序遍歷列印二叉樹的節點值
def print_tree_level_order(root):
if not root:
print("[]")
return
result = []
queue = deque([root])
while queue:
node = queue.popleft()
if node:
result.append(node.val)
queue.append(node.left)
queue.append(node.right)
else:
result.append(None)
# 去除結果列表末尾的 None
while result and result[-1] is None:
result.pop()
print(result)
# 示例
nodes = [1, None, 2, 3]
root = build_tree_iterative(nodes)
print_tree_level_order(root)
前序遍歷樹
from collections import deque
# 定義二叉樹節點的類
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
# 迭代方法將列表轉換為二叉樹
def build_tree_iterative(nodes):
if not nodes:
return None
root = TreeNode(nodes[0]) # 建立根節點
queue = deque([root]) # 使用佇列來追蹤節點
index = 1
while queue and index < len(nodes):
node = queue.popleft() # 取出佇列中的第一個節點
# 處理左子樹
if index < len(nodes) and nodes[index] is not None:
node.left = TreeNode(nodes[index])
queue.append(node.left) # 左子節點加入佇列
index += 1
# 處理右子樹
if index < len(nodes) and nodes[index] is not None:
node.right = TreeNode(nodes[index])
queue.append(node.right) # 右子節點加入佇列
index += 1
return root
# 前序遍歷列印二叉樹的節點值
def print_tree_preorder(root):
if not root:
print("[]")
return
result = []
stack = [root]
while stack:
node = stack.pop()
if node:
result.append(node.val)
stack.append(node.right) # 先右後左
stack.append(node.left) # 左子節點先入棧,確保左子樹優先處理
print(result)
# 示例
nodes = [1, None, 2, 3]
root = build_tree_iterative(nodes)
print_tree_preorder(root)
中序遍歷二叉樹
from collections import deque
# 定義二叉樹節點的類
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
# 迭代方法將列表轉換為二叉樹
def build_tree_iterative(nodes):
if not nodes:
return None
root = TreeNode(nodes[0]) # 建立根節點
queue = deque([root]) # 使用佇列來追蹤節點
index = 1
while queue and index < len(nodes):
node = queue.popleft() # 取出佇列中的第一個節點
# 處理左子樹
if index < len(nodes) and nodes[index] is not None:
node.left = TreeNode(nodes[index])
queue.append(node.left) # 左子節點加入佇列
index += 1
# 處理右子樹
if index < len(nodes) and nodes[index] is not None:
node.right = TreeNode(nodes[index])
queue.append(node.right) # 右子節點加入佇列
index += 1
return root
# 中序遍歷列印二叉樹的節點值
def print_tree_inorder(root):
if not root:
print("[]")
return
result = []
stack = []
current = root
while stack or current:
# 遍歷左子樹
while current:
stack.append(current)
current = current.left
# 處理當前節點
current = stack.pop()
result.append(current.val)
# 遍歷右子樹
current = current.right
print(result)
# 示例
nodes = [1, None, 2, 3]
root = build_tree_iterative(nodes)
print_tree_inorder(root)