前中后序三种遍历方法对于左右结点的遍历顺序都是一样的(先左后右),唯一不同的就是根节点的出现位置。
中序遍历:
先左再根后右
递归实现:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def MidTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
ans = []
def recurse(root):
if root:
recurse(root.left)
ans.append(root.val)
recurse(root.right)
recurse(root)
return ans
循环实现:
对于树的遍历,循环操作基本上要用到栈(stack)这个结构。对于中序遍历的循环实现,每次将当前结点(curr)的左子结点push到栈中,直到当前结点(curr)为None。这时,pop出栈顶的第一个元素,设其为当前结点,并输出该结点的value值,且开始遍历该结点的右子树。
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def MidTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
stack = []
ans = []
curr = root
while stack or curr:
if curr:
stack.append(curr)
curr = curr.left
else:
curr = stack.pop()
ans.append(curr.val)
curr = curr.right
return ans
先序遍历:
先根再左后右
递归实现:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def preorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
ans = []
def recurse(root):
if root != None:
ans.append(root.val)
recurse(root.left)
recurse(root.right)
recurse(root)
return ans
循环实现:
先看先序遍历。我们仍然使用栈stack,由于前序遍历的顺序是根左右,所以我们每次先打印当前结点curr,并将当前结点push到栈中,然后将左子结点设为当前结点,直到当前结点(curr)为None。pop出栈顶的第一个元素,设其为当前结点,开始遍历该结点的右子树。入栈和出栈条件以及循环结束条件与中序遍历一模一样。
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def preorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
stack = []
ans = []
curr = root
while stack or curr:
if curr:
ans.append(curr.val)
stack.append(curr)
curr = curr.left
else:
curr = stack.pop()
curr = curr.right
return ans
后序遍历:
先左再右后根
递归实现:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def postorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
ans = []
def recurse(root):
if root:
recurse(root.left)
recurse(root.right)
ans.append(root.val)
recurse(root)
return ans
循环实现:
由于后序遍历的顺序是左右中,我们把它反过来,则遍历顺序变成中右左,再逆序。
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def postorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
stack = []
ans = []
curr = root
while stack or curr:
if curr:
ans.append(curr.val)
stack.append(curr)
curr = curr.right
else:
curr = stack.pop()
curr = curr.left
return ans[::-1]
层次遍历:
层序遍历也可以叫做宽度优先遍历:先访问树的第一层结点,再访问树的第二层结点...然后一直访问到最下面一层结点。在同一层结点中,以从左到右的顺序依次访问。
二叉树的按层输出即从上往下、从左至右依次打印树的节点。每一层输出一行。
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def levelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
ans = []
queue = [root]
if not root:
return []
while queue:
temp = []
i = 0
numQueue = len(queue) #记录当前层中的结点个数
while i < numQueue: #遍历每一层
curr = queue.pop(0)
temp.append(curr.val)
if curr.left:
queue.append(curr.left)
if curr.right:
queue.append(curr.right)
i += 1
ans.append(temp)
return ans