实现二叉树先序、中序、后序遍历
Last updated
Last updated
问题简述
给定一棵二叉树,分别按照二叉树先序,中序和后序打印所有的节点。
思路:DFS
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# @param root TreeNode类 the root of binary tree
# @return int整型二维数组
#
class Solution:
def threeOrders(self , root: TreeNode) -> List[List[int]]:
# write code here
p, i, s = [], [], []
def dfs(x):
if x is None:
return
p.append(x.val)
dfs(x.left)
i.append(x.val)
dfs(x.right)
s.append(x.val)
dfs(root)
return [p, i, s]