写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.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. Redis 如何实现持久化

    1.RDB 持久化,将 Redis 在内存中的的状态保存到硬盘中,相当于备份数据库状态. 2.AOF 持久化(Append-Only-File),AOF 持久化是通过保存 Redis 服务器锁执行的写 ...

  2. LNMP环境搭建:Nginx安装、测试与域名配置

    Nginx作为一款优秀的Web Server软件同时也是一款优秀的负载均衡或前端反向代理.缓存服务软件 2.编译安装Nginx (1)安装Nginx依赖函数库pcre pcre为“perl兼容正则表达 ...

  3. vue-地址插件 v-region

    demo地址:https://terryz.gitee.io/vue/#/region/demo

  4. spring boot 添加拦截器的简单实例(springBoot 2.x版本,添加拦截器,静态资源不可访问解决方法)

    spring中拦截器主要分两种,一个是HandlerInterceptor,一个是MethodInterceptor 一.HandlerInterceptor HandlerInterceptor是s ...

  5. Java中CountDownLatch和CyclicBarrier的使用和比较

    CountDownLatch和CyclicBarrier同为Java1.5开始引入的,应用于多线程编程中的一种工具,二者用途十分相近,十分容易混淆. CountDownLatch CountDownL ...

  6. linux基础命令学习笔记(一)

    2019年4月1日: “目录” = “文件夹” 常用命令(一): 1.ls: list 列表,默认当前文件夹的文件和目录 linux:命令+选项+参数 ls -l:长输出,列出文件的详细信息 - rw ...

  7. kth min_max容斥

    题解: 普通的$min-max$容斥是用来求最后出现元素的期望之类的 $kth min-max$容斥进一步解决倒数第k个出现的元素的期望 给出公式: $kthmax(S)=\sum\limits_{T ...

  8. Scala数组| 集合

    arrays :+ 5尾部   头部5 +: arrays TODO 声明不可变数组,不能删; 默认情况下,scala中集合的声明全都是不可变的 val arrays: Array[Int] = Ar ...

  9. 对屏幕的理解---分辨率,dpi,ppi,屏幕尺寸,像素 等

    1. 名词理解 屏幕尺寸(screen size) 屏幕尺寸(screen size),是屏幕的对角线长度,一般讲的大小单位都是英寸. DPI (dots per inch) dpi 是(英文Dots ...

  10. 我的第二本译作《精通OpenStack》上架啦:书籍介绍和译者序

    1. 书籍简介 英文书名:Mastering OpenStack Second Edition 作者:[德] 奥马尔-海德希尔(Omar Khedher)[印] 坚登-杜塔-乔杜里(Chanda Du ...