题解 | 实现二叉树先序,中序和后序遍历 | 直接三次递归
实现二叉树先序,中序和后序遍历
http://www.nowcoder.com/practice/a9fec6c46a684ad5a3abd4e365a9d362
憨憨解法,三种遍历写三种递归
# 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
def pre(root,res):
if not root:
return
res.append(root.val)
pre(root.left,res)
pre(root.right,res)
def inner(root,res):
if not root:
return
inner(root.left, res)
res.append(root.val)
inner(root.right, res)
def after(root,res):
if not root:
return
after(root.left, res)
after(root.right, res)
res.append(root.val)
res=[]
a,b,c=[],[],[]
pre(root, a)
inner(root, b)
after(root, c)
res.extend([a,b,c])
return res