【C++】二叉树的构建、前序遍历、中序遍历
写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.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++】二叉树的构建、前序遍历、中序遍历的更多相关文章
- LeetCode:105_Construct Binary Tree from Preorder and Inorder Traversal | 根据前序和中序遍历构建二叉树 | Medium
要求:通过二叉树的前序和中序遍历序列构建一颗二叉树 代码如下: struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode ...
- 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 ...
- 【2】【leetcode-105,106】 从前序与中序遍历序列构造二叉树,从中序与后序遍历序列构造二叉树
105. 从前序与中序遍历序列构造二叉树 (没思路,典型记住思路好做) 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [ ...
- Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序遍历序列构造二叉树
Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序遍历序列构造二叉树 Leetcode:105. 从前序与中序遍历序列构造二叉树&106. 从中序与后序 ...
- Java实现 LeetCode 105 从前序与中序遍历序列构造二叉树
105. 从前序与中序遍历序列构造二叉树 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中 ...
- [LeetCode]105. 从前序与中序遍历序列构造二叉树(递归)、108. 将有序数组转换为二叉搜索树(递归、二分)
题目 05. 从前序与中序遍历序列构造二叉树 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 题解 使用HashMap记录当前子树根节点在中序遍历中的位置,方便每次 ...
- LeetCode---105. 从前序与中序遍历序列构造二叉树 (Medium)
题目:105. 从前序与中序遍历序列构造二叉树 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7 ...
- [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 ...
- leetcode 105 106 从前序与中序遍历序列构造二叉树 从中序与后序遍历序列构造二叉树
题目: 105 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = ...
- 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 ...
随机推荐
- Python-Django 模板层
1 模版简介 DTL:django模板语言 核心: 变量 {{ }} 标签 {% %} 2 模版语法之变量 -变量渲染:{{变量}} -变量深度查询:{{变量.索引/key值/方法}} <h2& ...
- js操作:selenium无法操作隐藏元素问题
对于前端隐藏元素,一直是selenium自动化定位元素的隐形杀手,脚本跑到隐藏元素时位置时报各种各样的错误, 隐藏的下拉菜单又没有办法避免,此帖只为交流隐藏元素自动化定位处理方法(3种操作) ...
- SSH阅读笔记
1.SSH单阶人脸段检测器,在不同层检测不同scale的人脸,而不是使用mtcnn中金字塔的方式,从而实现加速. 2.SSH的整体结构,3个module的stride分别为8,16,32,使用不同的感 ...
- django 实战篇之路由层
路由层 如何给网页添加首页及尾页 urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'test',views.test), url(r'te ...
- 阿里云各Linux发行版netcore兼容性评估报告---来自大石头的测试
阿里云各Linux发行版netcore兼容性评估报告---来自大石头的测试 结论: 优先选择CentOS/Ubuntu,可选AliyunLinux(CentOS修改版) ...
- [转] Redux入门教程(快速上手)
学习前提 在我们开始以前,确保你熟悉以下知识: 函数式JavaScript 面向对象JavaScript JavaScript ES6 语法 同时,确保你的设备已经安装: NodeJS Yarn(或者 ...
- 常用的js正则验证整理
一.校验数字的js正则表达式 1 数字:^[0-9]*$ 2 n位的数字:^\d{n}$ 3 至少n位的数字:^\d{n,}$ 4 m-n位的数字:^\d{m,n}$ 5 零和非零开头的数字:^(0| ...
- C#使用FileSystemWatcher控件实现的文件监控功能示例
本文实例讲述了C#使用FileSystemWatcher控件实现的文件监控功能.分享给大家供大家参考,具体如下: FileSystemWatcher 可以使用FileSystemWatcher组件监视 ...
- lambda表达式——写多线程
JDK1.8 中Lambda 表达式的出现,基本可以取替原来的匿名类实现多线程的方式.下面列举常用的常用的三种情况. 一.普通开启异步线程 new Thread(() -> System.o ...
- UOJ#460. 新年的拯救计划 构造
原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ460.html 题解 本题的构造方法很多.这里只介绍一种. 首先,总边数为 $\frac{n(n-1)}2 ...