题目链接:144. 二叉树的前序遍历 - 力扣(LeetCode)
先序遍历
递归写法:
class Solution:
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
result = []
def traversal(root: Optional[TreeNode]):
if root == None:
return
result.append(root.val)
traversal(root.left)
traversal(root.right)
traversal(root)
return result
迭代写法:
class Solution:
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if root == None:
return []
result = []
help_stack = []
help_stack.append(root)
while len(help_stack) > 0:
current = help_stack.pop()
result.append(current.val)# 前序遍历是根左右, 所以出栈顺序要先根后左右,所以入栈时先入栈右节点,这样先出栈的就是左节点
if current.right != None:
help_stack.append(current.right)
if current.left != None:
help_stack.append(current.left)
return result
统一迭代:
class Solution:
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if root == None:
return []
result = []
help_stack = []
help_stack.append(root)
while len(help_stack) > 0:
current = help_stack[-1]
if current != None:
help_stack.pop()
if current.right != None:
help_stack.append(current.right)
if current.left != None:
help_stack.append(current.left)
help_stack.append(current)
help_stack.append(None)
else:
help_stack.pop()
result.append(help_stack.pop().val)
return result
后序遍历
递归写法:
class Solution:
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
result = []
def traversal(root: Optional[TreeNode]):
if root == None:
return
traversal(root.left)
traversal(root.right)
result.append(root.val)
traversal(root)
return result
迭代写法:
class Solution:
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if root == None:
return []
result = []
help_stack = []
help_stack.append(root)
while len(help_stack) > 0:
current = help_stack.pop()
result.append(current.val) #因为后序遍历的顺序是左右中,由先序可得中左右,可调整为中右左,此时的顺序就是后序遍历的reverse,将结果reverse即可
if current.left != None:
help_stack.append(current.left)
if current.right != None:
help_stack.append(current.right)
result.reverse()
return result
统一迭代:
class Solution:
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if root == None:
return []
result = []
help_stack = []
help_stack.append(root)
while len(help_stack) > 0:
current = help_stack[-1]
if current != None:
help_stack.pop()
help_stack.append(current)
help_stack.append(None)
if current.right != None:
help_stack.append(current.right)
if current.left != None:
help_stack.append(current.left)
else:
help_stack.pop()
result.append(help_stack.pop().val)
return result
中序遍历
递归写法:
class Solution:
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
result = []
def traversal(root: Optional[TreeNode]):
if root == None:
return
traversal(root.left)
result.append(root.val)
traversal(root.right)
traversal(root)
return result
迭代写法:
class Solution:
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if root == None:
return []
result = []
help_stack = []
current = root # 指针,用来指出中序遍历的访问顺序
while current != None or len(help_stack) > 0:
if current != None:
help_stack.append(current)
current = current.left
else:
current = help_stack.pop() # 更新current节点
result.append(current.val)
current = current.right # 更新为右边的节点 (左 中 右)
return result
统一迭代:
class Solution:
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
if root == None:
return []
result = []
help_stack = []
help_stack.append(root)
while len(help_stack) > 0:
current = help_stack[-1]
if current != None: # 这里处理访问顺序
help_stack.pop() # 将当前节点弹出 并重新加入栈加标记 从而让处理顺序和访问顺序一致
if current.right != None:
help_stack.append(current.right)
help_stack.append(current)
help_stack.append(None)
if current.left != None:
help_stack.append(current.left)
else:
help_stack.pop() # 先将空节点弹出
cur_val = help_stack.pop().val
result.append(cur_val)
return result
层序遍历
题目链接:102. 二叉树的层序遍历 - 力扣(LeetCode)模板
class Solution:
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
if root == None:
return []
result = []
help_queue = deque()
help_queue.append(root)
while len(help_queue) > 0:
length = len(help_queue)
temp = []
for i in range(length):
cur = help_queue.popleft()
temp.append(cur.val)
if cur.left != None:
help_queue.append(cur.left)
if cur.right != None:
help_queue.append(cur.right)
result.append(temp)
return result