先序中序确定后序

// 中序先序确定后序.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <cstdio>
#include <stack>
#include <algorithm>
using namespace std;

const int maxn = 50;
struct node
{
    int data;
    node* lchild;
    node* rchild;
};

int pre[maxn], in[maxn], post[maxn];
int n;

node* create(int preL, int preR, int inL, int inR) {
    if (preL>preR)
    {
        return NULL;
    }
    node* root = new node;
    root->data = pre[preL];
    int i;
    for (i = inL; i < inR; i++)
    {
        if (in[i]==root->data)
        {
            break;
        }
    }
    int numsleft = i - inL;
    root->lchild = create(preL + 1, preL + numsleft, inL, i - 1);
    root->rchild = create(preL + numsleft + 1, preR, i + 1, inR);
    return root;
}
int num = 0;//已输出节点个数
void postorder(node* root) {
    if (root==NULL)
    {
        return;
    }
    postorder(root->lchild);
    postorder(root->rchild);
    printf("%d", root->data);
    num++;
}
int main()
{
    scanf_s("%d", &n);
    char str[10];
    stack <int> st;
    int x(0), preindex(0), inindex(0);//入栈元素,先序序列位置,以及中序序列位置
    for (int i = 0; i < 2*n; i++)
    {
        scanf_s("%s", str,9);
        if(strcmp(str,"push")==0){//push的次序就是先根遍历的次序
            scanf_s("%d", &x);
            pre[preindex++] = x;
            st.push(x);
        }
        else//pop的次序就是中序遍历的次序
        {
            in[inindex++] = st.top();
            st.pop();
        }
    }
    node* root = create(0, n - 1, 0, n - 1);
    postorder(root);
    return 0;
}

 

### 根据、中后序遍历还原二叉树的结构 在数据结构中,二叉树可以通过不同的遍历方式(、中后序)来还原其结构。每种组合都有特定的方法。 #### 1. + 中 遍历还原二叉树 根据遍历的第一个元素建立根节点,在中遍历中找到该元素,确定根节点的左右子树的中列;在列中确定左右子树的列;由左子树的列和中列建立左子树;由右子树的列和中列建立右子树 [^3]。 例如: - 已知一棵二叉树的遍历列为 `abdgcefh`。 - 中遍历列为 `dgbaechf`。 通过上述步骤可以逐步还原出二叉树的结构。 #### 2. 中 + 后序 遍历还原二叉树 根据后序遍历的最后一个元素建立根节点,在中遍历中找到该元素,确定根节点的左右子树的中列;在后序列中确定左右子树的后序列;由左子树的中后序列建立左子树;由右子树的中后序列建立右子树 [^1]。 例如: - 已知一棵二叉树的中遍历列为 `dgbaechf`。 - 后序遍历列为 `gdbefcah`。 通过上述步骤可以逐步还原出二叉树的结构。 #### 3. + 后序 遍历还原二叉树 虽然理论上后序遍历可以唯一确定一棵二叉树的结构,但在实际操作中通常需要额外的信息来辅助,因为单独使用后序遍历无法直接确定根节点的左右子树边界。因此,通常会结合其他方法或假设来完成还原过程 [^2]。 ### 示例代码:根据中后序遍历重建二叉树 ```python class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def build_tree(inorder, postorder): if not inorder or not postorder: return None root_val = postorder.pop() root = TreeNode(root_val) inorder_index = inorder.index(root_val) root.right = build_tree(inorder[inorder_index+1:], postorder) # 注意这里构建右子树 root.left = build_tree(inorder[:inorder_index], postorder) return root # 示例用法 inorder = ['d', 'g', 'b', 'a', 'e', 'c', 'h', 'f'] postorder = ['g', 'd', 'b', 'e', 'f', 'c', 'a', 'h'] root = build_tree(inorder, postorder) ``` ### 相关问题 1. 如何根据中后序遍历结果求二叉树的深度? 2. 在已知和中遍历的情况下,如何验证重建的二叉树是否正确? 3. 如果只知道后序遍历结果,能否唯一确定一棵二叉树?为什么? 4. 如何根据重建的二叉树进行层遍历? 5. 在Python中,如何可视化重建后的二叉树?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值