基础知识点可以看这篇文章:数据结构和算法 从0到1 :二叉树和二叉搜索树
先掌握递归的写法,然后再掌握非递归的写法:用栈(先进先出)
一、前序遍历
根左右,先访问根结点,然后依次访问左右子树。
1、递归写法:
思路:
先访问根结点,然后递归访问左子树,递归访问右子树
代码如下:
class TreeNode:
def __init__(self,x):
self.val = x
self.left = None
self.right = None
class preInPosTraversal:
#二叉树的前序、中序、后序遍历,递归
#前序:先根,然后左子树,然后右子树,每课子树都递归
def preOrderTraversal(self,head):
if not head:
return
print(head.val + " ")
self.preOrder(head.left)
self.preOrder(head.right)
或者:
class Solution:
def preorderTraversal(self, root: TreeNode) -> List[int]:
#递归
if not root :
return []
self.res = []
self.preorder(root)
return self.res
def preorder(self,root):
if not root:return
self.res.append(root.val)
self.preorder(root.left)
self.preorder(root.right)
2、迭代
思路:
在A的两颗子树中,遍历完左子树后,再遍历右子树,因此访问完根节点后,遍历左子树之前,要将右子树压入栈
代码如下:
class Solution:
def preOrderTraversal(self,head):
stack = []
res = []
node = head
while node or stack:
while node:
res.append(node.val)
stack.append(node.right)
node = node.left
node = stack.pop()
return res
或者:
class Solution:
def preOrderTraversal_2(self,head):
if not head:return
stack = []
res = []
node = head
stack.append(node)
while stack:
node = stack.pop()
res.append(node.val)
if node.right:
stack.append(node.right)
if node.left:
stack.append(node.left)
二、中序遍历
左根右,先访问左子树,再访问根节点,然后再访问右子树。
1、递归
思路:
先递归访问左节点,然后访问根节点,最后递归访问右节点。
代码如下:
class preInPosTraversal:
def inOrderTraversal(self,head):
if not head:
return
self.preOrder(head.left)
print(head.val + " ")
self.preOrder(head.right)
或者:
class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
#递归
if not root :
return []
self.res = []
self.inorder(root)
return self.res
def inorder(self,root):
if not root:return
self.inorder(root.left)
self.res.append(root.val)
self.inorder(root.right)
2、迭代
思路:
先把左子树压入栈,然后出栈,访问,然后遍历右子树
代码如下:
class Solution:
def inOrderTraversal(self,head):
stack,res = [],[]
node = head
while node or stack:
while node:
stack.append(node)
node = node.left
node = stack.pop()
res.append(node.val)
node = node.right
return res
或者:
class Solution:
def inOrderTraversal_2(self,head):
if not head:return
stack,res = [],[]
node = head
while stack or node:
if node:
stack.append(node)
node = node.left
else:
node = stack.pop()
res.append(node.val)
node = node.right
三、后序遍历
根左右,先访问左子树,然后访问右子树,最后访问根节点
1、递归
思路:
先递归左节点,在递归右节点,最后访问根节点
代码如下:
class preInPosTraversal:
#二叉树的前序、中序、后序遍历,递归
#前序:先根,然后左子树,然后右子树,每课子树都递归
def posterOrderTraversal(self,head):
if not head:
return
self.preOrder(head.left)
self.preOrder(head.right)
print(head.val + " ")
或者:
class Solution:
def posterTraversal(self,root):
if not root:
return []
self.res = []
self.poster(root)
return self.res
def poster(self,root):
if not root:return
self.poster(root.left)
self.poster(root.right)
self.res.append(root.val)
2、迭代
思路:
先访问根节点,把左子树入栈,再次遍历右子树;遍历完成后,把序列逆序
代码如下:
class Solution:
def postorOrderTraversal(self,head):
stack,res = [],[]
node = head
while node or stack:
while node:
res.append(node.val)
stack.append(node.left)
node = node.right
node = stack.pop()
res.reverse()
return res
或者:
class Solution:
def postorOrderTraversal_2(self,head):
if not head:return
stack,res = [],[]
node = head
while stack:
node = stack.pop()
res.append(node.val)
if node.left:
stack.append(node.left)
if node.right:
stack.append(node.right)
res.reverse()
return res
或者:
class Solution:
def postorOrderTraversal_2_2(self,head):
if not head:return
stack,res = [],[]
stack.append(head)
while stack:
node = stack[-1]
if node.left and head != node.left and head != node.right:
stack.append(node.left)
elif node.right and head != node.right:
stack.append(node.right)
else:
res.append(stack.pop())
head = node
return res