144. 二叉树的前序遍历
- 递归思路:
-
确定递归函数的参数和返回值
-
确定递归函数的终止条件
-
确定单层递归的逻辑
-
-
前序遍历顺序: 中左右
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def preorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
res = []
def traversal(node):
# when current pointer reach None, it menas end
if node == None:
return
# iterate order: middle;left;right
res.append(node.val) # middle
traversal(node.left) # left
traversal(node.right) # right
traversal(root)
return res
145. 二叉树后序遍历
- 后续遍历的顺序: 左右中
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def postorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
# create an array store value
res = []
def dfs(node):
# if node met none, return
if node == None:
return
# postorder traversal order: left right middle
dfs(node.left) # left
dfs(node.right) # right
res.append(node.val) # middle
dfs(root)
return res
94. 二叉树中序遍历
- 中序遍历的顺序: 左中右
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
# create an array
res = []
def dfs(node):
# if reach to the end, return None
if node == None:
return
# Inorder Traversal order: left middle right
dfs(node.left) # left
res.append(node.val) # middle
dfs(node.right) # right
dfs(root)
return res
层序遍历
-
我们通过记录队列的size来得知每一层应该弹出以及应该保存的元素有几个
-
如果root为空;则返回空数组
-
遍历队列,直到队列为空为止
-
创建一个数组用来存储每一层元素,最后的结果应该是一个二维数组
-
与此同时定义一个变量size来记录每一层队列的长度
-
由于我们在遍历树的时候在某一层会有两层元素混进来,所以这里的size至关重要,一定是要用变量保存的,因为下一层循环由于不同节点的元素情况不同可能导致size还在变动,所以下一层while循环的条件一定使用size存储的
-
这里的size可以告诉我们每一层有多少个元素,以及我们应该从队列弹出几个元素
-
102. 二叉树层序遍历
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def levelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
# create an array to store are final answer
result = []
# detemine the root is empty or not
if not root:
# we don't have root return empty array
return result
# create a queue
queue = collections.deque([root])
# traversal until the queue doesn't have any element
while queue:
# create a array store are each level elements
level = []
# use size to detemine which element belong to which level
size = len(queue)
# start record each level element
for _ in range(size):
# get first element in deque and pop it
cur = queue.popleft()
# store cur element into are level array
level.append(cur.val)
# detemine the left side is empty or not
if cur.left:
# it's not empty, add this element into queue
queue.append(cur.left)
# check right side
if cur.right:
queue.append(cur.right)
# let result store our level array
result.append(level)
# return our final result
return result
学习完这一题后也剩下的这些题也可以一起做:
- 107.二叉树的层次遍历II(opens new window)
- 199.二叉树的右视图(opens new window)
- 637.二叉树的层平均值(opens new window)
- 429.N叉树的层序遍历(opens new window)
- 515.在每个树行中找最大值(opens new window)
- 116.填充每个节点的下一个右侧节点指针(opens new window)
- 117.填充每个节点的下一个右侧节点指针II(opens new window)
- 104.二叉树的最大深度(opens new window)
- 111.二叉树的最小深度