题解 | #实现二叉树先序,中序和后序遍历#

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

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整型二维数组
#
firstArr = []
midArr = []
lastArr = []
class Solution:
    def threeOrders(self , root ):
        # write code here
        res = []
        #前序遍历
        res.append(self.firstSearch(root))
        res.append(self.midSearch(root))
        res.append(self.lastSearch(root))
        return res

    def firstSearch(self, root):

        if root != None:
            firstArr.append(root.val)
            self.firstSearch(root.left)
            self.firstSearch(root.right)
        return firstArr

    def midSearch(self, root):
        if root != None:
            self.midSearch(root.left)
            midArr.append(root.val)
            self.midSearch(root.right)
        return midArr

    def lastSearch(self, root):

        if root != None:
            self.lastSearch(root.left)
            self.lastSearch(root.right)
            lastArr.append(root.val)
        return lastArr
全部评论

相关推荐

白火同学:能。我当初应届沟通了1200,收简历50,面试10左右吧,加油投吧
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
2
分享

创作者周榜

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