根据先序中序遍历建树【模板】

简介:

主要就是通过先序找到当前子树根节点,再用中序遍历分子树,不断递归下去。

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <cassert>
#include <time.h>
#include <queue>
#include <map>
#include <stack>
#include <bitset>
#include <string>
#include <sstream>
#define INF 0x3f3f3f3f

using namespace std;

template <class Type>
Type stringToNum(const string& str)
{
    istringstream iss(str);
    Type num;
    iss >> num;
    return num;    
}

//======================================================

typedef struct node
{
    int m_key;
    node * m_pLeft,*m_pRight;
}BinaryTreeNode;

//BinaryTreeNode* constructTree(int* preOrder,int preLen,int* sMidOrder, int midLen){
BinaryTreeNode* constructRoot(int* preOrder, int* midOrder, int len){

    //先根遍历(前序遍历)的第一个值就是根节点的key
    int rootKey=preOrder[0];
    BinaryTreeNode* root=new BinaryTreeNode;
    root->m_key=rootKey;
    root->m_pLeft=root->m_pRight=NULL;
    if(len==1 && *preOrder==*midOrder)//只有一个节点
        return root;

    //在中根遍历(中序遍历)中找到根节点
    int* rootMidOrder=midOrder;
    int leftLen=0; //左子树节点数
    while(*rootMidOrder!=rootKey&&rootMidOrder<=(midOrder+len-1)){ 
        ++rootMidOrder;
        ++leftLen;
    }
    if(*rootMidOrder!=rootKey)//在中根序列未找到根节点,输入错误
        return NULL;

    if(leftLen>0){ //构建左子树
        root->m_pLeft=constructRoot(preOrder+1,midOrder,leftLen);
    }
    if(len-leftLen-1>0){ //构建右子树
        root->m_pRight=constructRoot(preOrder+leftLen+1,rootMidOrder+1,len-leftLen-1);
    }
    return root;
}

BinaryTreeNode* construct(int* preOrder,int* midOrder,int len){
    if(preOrder==NULL||midOrder==NULL||len<=0)
        return NULL;
    return constructRoot(preOrder,midOrder,len);
}

//先根遍历
void preOrderRecursionPrint(BinaryTreeNode* root){
    if(root==NULL)
        return;
    cout<<root->m_key<<endl;   //visit
    preOrderRecursionPrint(root->m_pLeft);
    preOrderRecursionPrint(root->m_pRight);
}

int main()
{
    //freopen("input.txt","r",stdin);

    //先根序列
    int preOrder[8]={1,2,4,7,3,5,6,8};
    //中根序列
    int midOrder[8]={4,7,2,1,5,3,8,6};

    BinaryTreeNode * treeRoot = construct(preOrder,midOrder,8);

    preOrderRecursionPrint(treeRoot);

    return 0;
}

输出如下:

这里写图片描述

相关文章
|
C语言 数据安全/隐私保护
C语言练习题
C语言练习题
213 1
|
开发工具
MineCraft - 创世神插件
前言 本文将介绍Minecraft中的WorldEdit(创世神)插件。阅读完本文后将能够使用该插件提供的绝大部分功能。 本文不包含该插件的下载与安装,请确保自己在游戏中可以正常使用该插件后再阅读本文。
4851 0
|
API
《Adobe Illustrator CS4中文版经典教程》—第1课 了解工作区
<span style='letter-spacing:1px'>本节书摘来自异步社区《Adobe Illustrator CS4中文版经典教程》一书中的第1课了解工作区,作者【美】Adobe公司,更多章节内容可以访问云栖社区“异步社区”公众号查看。</span>
1945 0
OpenCascade Primitives BRep - Sphere
OpenCascade Primitives BRep - Sphere eryar@163.com Abstract. BRep is short for Boundary Representation.
1338 0
|
uml 测试技术
[UML]UML系列——协作图(通信图)collaboration diagram
原文:[UML]UML系列——协作图(通信图)collaboration diagram 系列文章 [UML]UML系列——用例图Use Case [UML]UML系列——用例图中的各种关系(include、extend) [UML]UML系列——类图Class [UML]UML系列——类图...
1835 0
|
1天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8059 2
|
10天前
|
域名解析 人工智能 Linux
|
20天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18329 63
|
18天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置