重建二叉树和遍历二叉树

在这里插入图片描述

class TreeNode:
     def __init__(self, x):
         self.val = x
         self.left = None
         self.right = None 
        
#1.由中序和前序重建二叉树,输出后序
#中:GDHBEAFC 前:ABDGHECF
def recreateTree(preorder,inorder):
    if len(preorder)<=0:
        return None
    else:
        root=TreeNode(preorder.pop(0))
        root_index=inorder.index(root.val)
        root.left=recreateTree(preorder[:root_index],inorder[:root_index])
        root.right=recreateTree(preorder[root_index:],inorder[root_index+1:])
    return root

def postorder(root):
    global res
    if root==None:
       return []
    else:
       postorder(root.left)
       postorder(root.right)
       res.append(root.val)

preorder='A B D G H E C F'.split(' ')
inorder='G D H B E A F C'.split(' ')
t=recreateTree(preorder,inorder)

res=[] 
postorder(t)   
print(','.join(str(i) for i in res))
    
#2.由中序和后序重建二叉树,输出前序
#中:GDHBEAFC 前:ABDGHECF 后:GHDEBFCA 
def recreateTree(postorder,inorder):
     if len(postorder)<=0:
         return None
     else:
         root=TreeNode(postorder.pop(-1))
         root_index=inorder.index(root.val)
         root.left=recreateTree(postorder[:root_index],inorder[:root_index])
         root.right=recreateTree(postorder[root_index:],inorder[root_index+1:])
     return root

def preorder(root):
    global res
    if root==None:
        return []
    else:
        res.append(root.val)
        preorder(root.left)
        preorder(root.right)
        
postorder='G H D E B F C A'.split(' ')
inorder='G D H B E A F C'.split(' ')
t=recreateTree(postorder,inorder)

res=[] 
preorder(t)   
print(','.join(str(i) for i in res))

#3.已知前序和后序,不能重建二叉树,因为前序和后序不知道树的左右关系;
#由前序和后序,可以有几种中序的输出方法?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉树遍历是指按照一定的顺序访问二叉树中的所有节点,常见的遍历方式有前序遍历、中序遍历和后序遍历重建二叉树是指根据二叉树遍历结果,重建出原来的二叉树结构。 下面是一个基于C++实现的二叉树遍历和重建的实验分析: 1. 实验环境 - 操作系统:Windows 10 - 开发环境:Visual Studio 2019 - 编程语言:C++ 2. 实验过程 2.1 二叉树遍历 在实验中,我们采用前序遍历的方式来遍历二叉树。前序遍历的过程如下: - 访问根节点 - 前序遍历左子树 - 前序遍历右子树 实现前序遍历的代码如下: ```c++ void preOrder(TreeNode* root) { if(root == nullptr) return; cout << root->val << " "; preOrder(root->left); preOrder(root->right); } ``` 2.2 二叉树的重建 在实验中,我们采用前序遍历和中序遍历的结果来重建二叉树重建二叉树的过程如下: - 根据前序遍历结果确定根节点 - 在中序遍历结果中找到根节点的位置 - 根据中序遍历结果将左子树和右子树分开 - 递归重建左子树和右子树 实现重建二叉树的代码如下: ```c++ TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { if(preorder.empty() || inorder.empty()) return nullptr; TreeNode* root = new TreeNode(preorder[0]); int idx = 0; while(inorder[idx] != preorder[0]) ++idx; vector<int> left_inorder(inorder.begin(), inorder.begin() + idx); vector<int> right_inorder(inorder.begin() + idx + 1, inorder.end()); vector<int> left_preorder(preorder.begin() + 1, preorder.begin() + 1 + idx); vector<int> right_preorder(preorder.begin() + 1 + idx, preorder.end()); root->left = buildTree(left_preorder, left_inorder); root->right = buildTree(right_preorder, right_inorder); return root; } ``` 3. 实验结论 通过本次实验,我们能够了解到二叉树遍历和重建的实现方法,以及相关的算法原理。同时,我们也能够学习到如何利用递归算法进行二叉树遍历和重建,加深对递归算法的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值