利用前序遍历和中序遍历重建二叉树

本段代码从列表建立二叉树——生成前序遍历和中序遍历——重建二叉树。

重建二叉树时要注意一个条件,也就是前序遍历list长度大于0的条件。

重建二叉树的github地址

class TreeNode:
    def __init__(self, x = -1, left = None, right = None):
        self.val = x
        self.left = left
        self.right = right
        
# 利用列表构造二叉树     
class BinaryTree(object):
    def __init__(self,root = None):
        self.root = root
    def buildTree(self, root, lis, i=0):
        if i < len(lis):
            if lis[i] == '#':
                return None
            else:
                root = TreeNode(x = lis[i])
                root.left = self.buildTree(root.left, lis, 2*i+1)
                root.right = self.buildTree(root.right, lis, 2*i+2)
                return root
        return root
    
# 利用前序遍历和中序遍历重建二叉树
class RebuilBinaryTree(object):
    def reBuildBtree(self, preorder, inorder):
        if preorder == None or inorder == None:
            return None
#         保证长度大于0
        if len(preorder) > 0:
            root = TreeNode(preorder[0])
            root_id = inorder.index(root.val)
    #         重建左子树
            root.left = self.reBuildBtree(preorder[1:root_id + 1], inorder[:root_id])
    #     重建右子树
            root.right = self.reBuildBtree(preorder[root_id+1:], inorder[root_id+1:])
            return root
        
# 宽度优先遍历 打印二叉树
    def printBFS(self,root):
        if root == None:
            return None
#         queen = []
        res = []
        queen = [root]
        while queen:
            nowNode = queen.pop(0)
            res.append(nowNode.val)
            if nowNode.left != None:
                queen.append(nowNode.left)
            if nowNode.right != None:
                queen.append(nowNode.right)
        return res
    
# 前序优先遍历
class Preorder_iter(object):
    def preorderTraversal(self, root):
        if root == None: return None
        stack = [root]
        res = []
        while stack:
            root = stack.pop()
            res.append(root.val)
            if root.right != None:
                stack.append(root.right)
            if root.left != None:
                stack.append(root.left)
        return res
    
#中序优先遍历
class Inorder_iter(object):
    def inorderTraversal(self, root):
        if root == None:
            return []
        stack = []
        res = []
        cur = root
        while cur or stack:
            while cur:
                stack.append(cur)
                cur = cur.left
            cur = stack.pop()
            res.append(cur.val)
            cur = cur.right
        return res
if __name__ == '__main__':
    lis = [1, 2, 3, 4, '#', 5, 6, '#', 7, '#','#','#','#',8,'#']
    root = TreeNode()
    btree = BinaryTree().buildTree(root, lis, 0)
    BFSres = RebuilBinaryTree().printBFS(btree)
    print('BFS',BFSres)
    preorder = Preorder_iter().preorderTraversal(btree)
    print('preorder',preorder)
    inorder = Inorder_iter().inorderTraversal(btree)
    print('inorder',inorder)
    rebuildTree = RebuilBinaryTree().reBuildBtree(preorder, inorder)
    re_BFS = RebuilBinaryTree().printBFS(rebuildTree)
    print('rebuild tree bfs',re_BFS)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值