根据根结点被访问的时机,分为前序遍历(根、左子树、右子树)、中序遍历(左 子树、根、右子树)和后序遍历(左子树、右子树、根)。
结点表示集合 (set),边表示关系 (relationship)。
二叉树是理解高级数据结构的基础。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
result = []
if not root:
return []
def get_help(root):
if not root:
return
# 根左右
result.append(root.val)
get_help(root.left)
get_help(root.right)
get_help(root)
return result
"""
# Definition for a Node.
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children
"""
class Solution:
def preorder(self, root: 'Node') -> List[int]:
if not root:
return
result = []
queue = collections.deque()
queue.append(root)
while queue:
node = queue.pop()
result.append(node.val)
for child in node.children[::-1]:
queue.append(child)
return result
思路: 交换左右子树,再递归翻转左右子树。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if not root:
return
tmp = self.invertTree(root.left)
root.left = self.invertTree(root.right)
root.right = tmp
return root
使用将行号作为参数的递归即可。也可以使用队列 BFS 来进行层序遍历
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
if not root:
return []
result = []
queue = collections.deque()
queue.append(root)
while queue:
tmp = []
for _ in range(len(queue)):
node = queue.popleft()
tmp.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
result.append(tmp)
return result
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return []
result = []
queue = collections.deque()
queue.append(root)
while queue:
tmp = []
for _ in range(len(queue)):
node = queue.popleft()
tmp.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
result.append(tmp)
result = result[::-1]
return result
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return []
result = []
queue = collections.deque()
queue.append(root)
flag = True
while queue:
tmp = []
for _ in range(len(queue)):
node = queue.popleft()
tmp.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
if flag:
result.append(tmp)
else:
result.append(tmp[::-1])
flag = not flag
# result.append(tmp)
return result