二叉树先序:根左右 二叉树中序:左根右 二叉树后序:左右根 代码: # 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 ):
# write code here
pre_list,mid_list,aft_list=[],[],[]
def preorder(root):
if not root:
return None
pre_list.append(root.val)
preorder(root.left)
preorder(root.right)
def midorder(root):
if not root:
return None
midorder(root.left)
mid_list.append(root.val)
midorder(root.right)
def aftorder(root):
if not root:
return None
aftorder(root.left)
aftorder(root.right)
aft_list.append(root.val)
preorder(root)
midorder(root)
aftorder(root)
return [pre_list,mid_list,aft_list]
|