写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文!

本博客全网唯一合法URL:https://www.cnblogs.com/acm-icpcer/p/10404776.html

按前序遍历次序构建二叉树:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<fstream>
using namespace std; struct tnode
{
char data;
tnode *l,*r;
}; class tree
{
public: tnode *root; tree()
{
//root=NULL;
} tnode* getroot()
{
return this->root;
} bool build(tnode * & root,char *input,int & index)
{
if(index>=strlen(input))
{
return false;
}
if(input[index]=='#')
{
root=NULL;
index++;
}
else
{
root=new tnode;
root->data=input[index];
index++;
build(root->l,input,index);
build(root->r,input,index);
} } bool pre_display(tnode *t,fstream &f);
}; /*
bool tree::build()
{
root->data='a';
root->l=new tnode();
root->l->data='c';
root->r=new tnode();
root->r->data='b';
return true;
}
*/
/*
bool tree::build(tnode * & root,char *input,int & index)
{
if(index>=strlen(input))
{
return false;
}
if(input[index]=='#')
{
root=NULL;
index++;
}
else
{
root=new tnode;
root->data=input[index];
index++;
build(root->l,input,index);
build(root->r,input,index); }
}
*/ bool tree::pre_display(tnode *t,fstream &f)
{
if(t!=NULL)
{
f<<t->data<<endl;
cout<<t->data<<' ';
pre_display(t->l,f);
pre_display(t->r,f);
}
return true;
} /*
void preOrder(tnode * & root,char *input,int & index)
{
if(index>=strlen(input))
{
return ;
}
if(input[index]=='#')
{
root=NULL;
index++;
}
else
{
root=new tnode;
root->data=input[index];
index++;
preOrder(root->l,input,index);
preOrder(root->r,input,index); }
}
*/
//this function is not belongs to the tree class,writing for test purpose
void inOrder(tnode * root)
{
if(root==NULL)
{
return ;
}
inOrder(root->l);
cout<<root->data<<" ";
inOrder(root->r);
} int main()
{
fstream f("result.txt", ios::out); char buffer[];
memset(buffer,'\0',strlen(buffer));
tree *mt=new tree();
while(scanf("%s",&buffer))
{
int index=;
//cout<<mt->getroot()<<endl<<mt->root<<endl;
if(mt->build(mt->root,buffer,index))
{
inOrder(mt->getroot());
cout<<endl;
mt->pre_display(mt->getroot(),f);
}
} f.close();
return ;
}

代码运行说明:

手动按照前序输入字符串,每个字符代表一个节点,对于空节点则输入‘#’,程序会输出前序遍历结果和秩序遍历结果。

例如,对于二层满二叉树,输入前序遍历为:ab##c##

输出为:

第一行结果为中序遍历,第二行结果为前序遍历。

按行序遍历构建二叉树:

#include<iostream>
#include<cstring>
using namespace std;
const int M=; struct node{
char data;
node *l;
node *r;
}; void build(node * & root,char *input,int index)
{
if(index>=strlen(input))
return ;
if(input[index]=='#')
root=NULL;
else
{
root=new node();
root->data=input[index];
build(root->l,input,(index+)*-);
build(root->r,input,(index+)*);
}
} void pre_display(node *root)
{
if(root==NULL)
return ;
cout<<root->data<<" ";
pre_display(root->l);
pre_display(root->r);
} void in_display(node *root)
{
if(root==NULL)
return ;
in_display(root->l);
cout<<root->data<<" ";
in_display(root->r);
} int main()
{
node *tree1=new node();
char data[M];
memset(data,'\0',sizeof(data));
while(scanf("%s",data))
{
build(tree1,data,);
pre_display(tree1);
cout<<endl;
in_display(tree1);
} return ;
}

第一行结果为中序遍历,第二行结果为前序遍历。

tz@HZAU

2019/2/20

【C++】二叉树的构建、前序遍历、中序遍历的更多相关文章

  1. LeetCode:105_Construct Binary Tree from Preorder and Inorder Traversal | 根据前序和中序遍历构建二叉树 | Medium

    要求:通过二叉树的前序和中序遍历序列构建一颗二叉树 代码如下: struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode ...

  2. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  3. 【2】【leetcode-105,106】 从前序与中序遍历序列构造二叉树,从中序与后序遍历序列构造二叉树

    105. 从前序与中序遍历序列构造二叉树 (没思路,典型记住思路好做) 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [ ...

  4. Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序遍历序列构造二叉树

    Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序遍历序列构造二叉树 Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序 ...

  5. Java实现 LeetCode 105 从前序与中序遍历序列构造二叉树

    105. 从前序与中序遍历序列构造二叉树 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中 ...

  6. [LeetCode]105. 从前序与中序遍历序列构造二叉树(递归)、108. 将有序数组转换为二叉搜索树(递归、二分)

    题目 05. 从前序与中序遍历序列构造二叉树 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 题解 使用HashMap记录当前子树根节点在中序遍历中的位置,方便每次 ...

  7. LeetCode---105. 从前序与中序遍历序列构造二叉树 (Medium)

    题目:105. 从前序与中序遍历序列构造二叉树 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7 ...

  8. [Swift]LeetCode105. 从前序与中序遍历序列构造二叉树 | Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  9. leetcode 105 106 从前序与中序遍历序列构造二叉树 从中序与后序遍历序列构造二叉树

    题目: 105 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = ...

  10. leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)

    题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume t ...

随机推荐

  1. android系统添加预置APP(so库自动释放)

    将APK直接放入系统目录中,会导致APK找不到so文件.正常情况下的安装是使用PackageManager,它会将so文件拷贝到系统读取的so目录(system/lib或system/lib64)下, ...

  2. 第十三章 部署Java应用程序

    打包 清单文件被命名为MANIFEST.MF,用于描述归档特征. 清单文件被分为多个节, 第一节被称为主节,作用于整个文件; 其他节则必须起始于Name的条目. 节之间空行分开. jar cfm JA ...

  3. kettle 数据迁移

    最近在公司搞一个项目重构迁移问题,旧项目一直在线上跑,重构的项目则还没上线.重构之后数据库表结构,字段,类型等都有变化,而且重构的数据库由oracl改为mysql.这样就设计到数据迁移问题,别人推荐下 ...

  4. MySql常见的数据类型

    ⒈整型 名称 字节数 tinyint 1 smallint 2 mediumint 3 int/integer 4 bigint 8 特点: 1.如果不设置无符号还是有符号,默认是有符号,如果想设置无 ...

  5. Spring Cloud 2-Ribbon 客户端负载均衡(二)

    Spring Cloud Eureka  1.Hello-Service服务端配置 pom.xml application.yml 启动两个service 2.Ribbon客户端配置 pom.xml ...

  6. C# 对MongoDB 进行增删改查的简单操作

    C# 对MongoDB 进行增删改查的简单操作   下面演示下C#操作MongoDB驱动的简单的增删改查代码 运用到的MongoDB支持的C#驱动,当前版本为1.6.0 1,连接数据库   /// & ...

  7. 【原创】Linux基础之上传下载

    1 rz sz 安装 yum install -y lrzsz 上传 rz ,对话框操作 下载 sz $filename 注意:rz不能上传大于4g的文件,此时可以改为scp或sftp上传,其中sft ...

  8. 【原创】数据库基础之Mysql(1)常用命令

    1 创建用户 CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 比如 create user 'test_user'@'%' identi ...

  9. Deformable Convolutional Network

    Deformable conv赋予了CNN位置变换的能力,与普通卷积的对比如下.可以看到,在每个特征点上,会产生一个偏移,变成 不规则的卷积(感觉本质上,就是让CNN自己去学习感受野). 思想来源于S ...

  10. java设计模式自我总结---代理模式

    代理模式是给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用,通俗的来讲代理模式就是我们生活中常见的中介. Spring 的AOP面向切面就是使用动态代理模式来实现的: 打个比方说:我要买房 ...