题解 | 实现二叉树先序,中序和后序遍历 | 直接三次递归

实现二叉树先序,中序和后序遍历

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
全部评论

相关推荐

陆续:不可思议 竟然没那就话 那就我来吧 :你是我在牛客见到的最美的女孩
点赞 评论 收藏
分享
Yki_:你要算时间成本呀,研究生两三年,博士三四年,加起来就五六年了,如果你本科去腾讯干五年,多领五年的年薪,加上公司内涨薪,可能到时候十五年总薪资也跟博士差不多
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务