ACM题目————二叉树的遍历
一、二叉树的后序遍历:
给定一颗二叉树,要求输出二叉树的深度以及后序遍历二叉树得到的序列。本题假设二叉树的结点数不超过1000
输 入数据分为多组,第一行是测试数据的组数n,下面的n行分别代表一棵二叉树。每棵二叉树的结点均为正整数,数据为0代表当前结点为空,数据为-1代表二叉 树数据输入结束,-1不作处理。二叉树的构造按照层次顺序(即第1层1个整数,第2层2个,第3层4个,第4层有8个......,如果某个结点不存在以 0代替)。
输出每棵二叉树的深度以及后序遍历二叉树得到的序列。
2
1 -1
1 2 0 3 4 -1
1 1
3 3 4 2 1
//Asimple
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;const int maxn = ;
int n, T, num, cnt, point, line, x, y, t;
bool flag; typedef struct node
{
int data ;
struct node *lchild, *rchild;
}BiNode, *BiTree; BiTree *q[maxn]; int Deepth(BiTree T)
{
if( T == NULL ) return ;
int x = Deepth(T->lchild);
int y = Deepth(T->rchild);
return max(x,y)+ ;
} void Hou_Print(BiTree T)
{
if( T == NULL ) return ;
Hou_Print(T->lchild);
Hou_Print(T->rchild);
cout << " " << T->data ;
} int main()
{
BiTree u, v, root;
int f, r;
cin >> T ;
while( T -- )
{
flag = true ;
f = r = ;
while( scanf("%d",&num)&&num!=-)//建树
{
if( flag )//头节点
{
root = (BiTree)malloc(sizeof(BiNode));
root->data = num ;
root->lchild = root->rchild = NULL ;
if( root->data == )
{
root = NULL ;
cout << "0 0" << endl ;
break;
}
q[r++] = &root->lchild;
q[r++] = &root->rchild;
flag = false ;
}
else
{
u = (BiTree)malloc(sizeof(BiNode));
u->data = num ;
u->lchild = u->rchild = NULL ;
if( u->data != )
{
q[r++] = &u->lchild;
q[r++] = &u->rchild;
}
else u = NULL ;
*q[f++] = u ;
}
}
cnt = Deepth(root);
cout << cnt ;
Hou_Print(root);
cout << endl ;
} return ;
}
二、中序遍历二叉树
给定一颗二叉树,要求输出二叉树的深度以及中序遍历二叉树得到的序列。本题假设二叉树的结点数不超过1000。
输 入数据分为多组,第一行是测试数据的组数n,下面的n行分别代表一棵二叉树。每棵二叉树的结点均为正整数,数据为0代表当前结点为空,数据为-1代表二叉 树数据输入结束,-1不作处理。二叉树的构造按照层次顺序(即第1层1个整数,第2层2个,第3层4个,第4层有8个......,如果某个结点不存在以 0代替)
输出每棵二叉树的深度以及中序遍历二叉树得到的序列。
2
1 -1
1 2 0 3 4 -1
1 1
3 3 2 4 1
//Asimple
#include <stdio.h>
#include <iostream> using namespace std;const int maxn = ;
int n, T, num, cnt, point, line, x, y, t;
bool flag; typedef struct node
{
int data ;
struct node *lchild, *rchild;
}BiNode, *BiTree; BiTree *q[maxn]; int Deepth(BiTree T)
{
if( T == NULL ) return ;
int x = Deepth(T->lchild);
int y = Deepth(T->rchild);
return max(x,y)+ ;
} void Zhong_Print(BiTree T)
{
if( T == NULL ) return ;
Zhong_Print(T->lchild);
cout << " " << T->data ;
Zhong_Print(T->rchild);
} int main()
{
BiTree u, v, root;
int f, r;
cin >> T ;
while( T -- )
{
flag = true ;
f = r = ;
while( scanf("%d",&num)&&num!=-)//建树
{
if( flag )//头节点
{
root = (BiTree)malloc(sizeof(BiNode));
root->data = num ;
root->lchild = root->rchild = NULL ;
if( root->data == )
{
root = NULL ;
cout << "0 0" << endl ;
break;
}
q[r++] = &root->lchild;
q[r++] = &root->rchild;
flag = false ;
}
else
{
u = (BiTree)malloc(sizeof(BiNode));
u->data = num ;
u->lchild = u->rchild = NULL ;
if( u->data != )
{
q[r++] = &u->lchild;
q[r++] = &u->rchild;
}
else u = NULL ;
*q[f++] = u ;
}
}
cnt = Deepth(root);
cout << cnt ;
Zhong_Print(root);
cout << endl ;
} return ;
}
三、前序遍历:
给定一颗二叉树,要求输出二叉树的深度以及先序遍历二叉树得到的序列。本题假设二叉树的结点数不超过1000。
输入数据分为多组,第一行是测试数据的组数n,下面的n行分别代表一棵二叉树。每棵二叉树的结点均为正整数,数据为0代表当前结点为空,数据为-1 代表二叉树数据输入结束,-1不作处理。二叉树的构造按照层次顺序(即第1层1个整数,第2层2个,第3层4个,第4层有8个......,如果某个结点 不存在以0代替),
输出每棵二叉树的深度以及先序遍历二叉树得到的序列。
2
1 -1
1 2 0 3 4 -1
1 1
3 1 2 3 4
//Asimple
#include <stdio.h>
#include <iostream> using namespace std;const int maxn = ;
int n, T, num, cnt, point, line, x, y, t;
bool flag; typedef struct node
{
int data ;
struct node *lchild, *rchild;
}BiNode, *BiTree; BiTree *q[maxn]; int Deepth(BiTree T)
{
if( T == NULL ) return ;
int x = Deepth(T->lchild);
int y = Deepth(T->rchild);
return max(x,y)+ ;
} void Qian_Print(BiTree T)
{
if( T == NULL ) return ;
cout << " " << T->data ;
Qian_Print(T->lchild);
Qian_Print(T->rchild);
} int main()
{
BiTree u, v, root;
int f, r;
cin >> T ;
while( T -- )
{
flag = true ;
f = r = ;
while( scanf("%d",&num)&&num!=-)//建树
{
if( flag )//头节点
{
root = (BiTree)malloc(sizeof(BiNode));
root->data = num ;
root->lchild = root->rchild = NULL ;
if( root->data == )
{
root = NULL ;
cout << "0 0" << endl ;
break;
}
q[r++] = &root->lchild;
q[r++] = &root->rchild;
flag = false ;
}
else
{
u = (BiTree)malloc(sizeof(BiNode));
u->data = num ;
u->lchild = u->rchild = NULL ;
if( u->data != )
{
q[r++] = &u->lchild;
q[r++] = &u->rchild;
}
else u = NULL ;
*q[f++] = u ;
}
}
cnt = Deepth(root);
cout << cnt ;
Qian_Print(root);
cout << endl ;
} return ;
}
树。。
ACM题目————二叉树的遍历的更多相关文章
- ACM题目————二叉树最大宽度和高度
http://codevs.cn/problem/1501/ 题目描述 Description 给出一个二叉树,输出它的最大宽度和高度. 输入描述 Input Description 第一行一个整 ...
- [PTA] 数据结构与算法题目集 6-8 求二叉树高度 & 6-9 二叉树的遍历
6.8 二叉树高度 int GetHeight(BinTree BT) { if (BT == NULL) return 0; int leftH = GetHeight(BT->Left); ...
- ACM 重建二叉树
重建二叉树 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 题目很简单,给你一棵二叉树的后序和中序序列,求出它的前序序列(So easy!). 输入 输入有多组数 ...
- C++版 - 剑指Offer 面试题39:二叉树的深度(高度)(二叉树深度优先遍历dfs的应用) 题解
剑指Offer 面试题39:二叉树的深度(高度) 题目:输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.例如:输入二叉树 ...
- DS二叉树--层次遍历
题目描述 层次遍历二叉树,是从根结点开始遍历,按层次次序“自上而下,从左至右”访问树中的各结点. 建树方法采用“先序遍历+空树用0表示”的方法 要求:采用队列对象实现,函数框架如下: 输入 第一行输入 ...
- 二叉树的遍历 &【NOIP2001普及组】& 洛谷 P1030 求先序排列
题目链接 https://www.luogu.org/problemnew/show/P1030 模板题 先讲一下二叉树的遍历 二叉树的遍历 分类 性质 求法 分为三类: 先序遍历(PreOrder) ...
- 【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】
[107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a ...
- [LeetCode] 107. Binary Tree Level Order Traversal II 二叉树层序遍历 II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- 【数据结构】二叉树的遍历(前、中、后序及层次遍历)及leetcode107题python实现
文章目录 二叉树及遍历 二叉树概念 二叉树的遍历及python实现 二叉树的遍历 python实现 leetcode107题python实现 题目描述 python实现 二叉树及遍历 二叉树概念 二叉 ...
随机推荐
- iOS自动偏移64个像素
自从iOS7开始,如果添加的scrollview是uiviewController第一个视图,系统会默认自动添加-64的偏移量,所以规避的方案就添加一个UIView之后再添加你的scrollview.
- SQL Server 2012大数据导入Oracle的解决方案
在实际工作中需要把SQL Server中的表导入Oracle.之前尝试过直接用SQL Server的DTS进行都没问题.但这次因为数据量在千万级所以报虚拟内存不足.最后通过SQL Server导出tx ...
- Java基础之处理事件——使用适配器类(Sketcher 3 using an Adapter class)
控制台程序. 适配器类是指实现了监听器接口的类,但监听器接口中的方法没有内容,所以它们什么也不做.背后的思想是:允许从提供的适配器类派生自己的监听器类,之后再实现那些自己感兴趣的类.其他的空方法会从适 ...
- 理解运算符 || 和 && 及方法
|| 前面为true的话直接返回前面的值,前面为false的话返回后面的值.如下: console.log(0 || 1); console.log(1 || 0); console.log(1 || ...
- 转:Jmeter之Bean shell使用(二)
上一篇Jmeter之Bean shell使用(一)简单介绍了下Jmeter中的Bean shell,本文是对上文的一个补充,主要总结下常用的几种场景和方法,相信这些基本可以涵盖大部分的需求.本节内容如 ...
- SQL 面向对象(委托)
委托:也称为代理,事件也是一种委托:定义在类的最外面 1.定义委托关键字:delegate函数签名:签名和函数保持一致定义委托的时候要根据函数来定义public delegate int First( ...
- 通过JavaScript设置样式和jQuey设置样式,还有随机数抛出水果的习题
一:通过JavaScript的方式设置样式(:拿习题为例): var shuiguo = document.getElementById('fruit'); shuiguo.style.bac ...
- C#访问PostGreSQL数据库的方法 http://www.jb51.net/article/35643.htm
这次的项目中的一个环节要求我把PostGreSQL数据取出来,然后放到SqlServer里,再去处理分析. http://www.jb51.net/article/35643.htm - 我对Post ...
- 夺命雷公狗---2016-linux---1之ip的配置
在linux下输入以下命令即可配置成功, 但是前提是linux下的这个ip地址的ip段是通过本地ping出来的才可以,如下所示: 然后查看下是否配置成功: 已经配置成功了,那么下一步我们可以ping下 ...
- beta2阶段组员分数分配
小组名称:nice! 小组成员:李权 于淼 刘芳芳 韩媛媛 宫丽君 项目内容:约跑app 分数分配规则 个人贡献分=项目基础分*0.5+个人代码贡献量*0.5 基本贡献分 个人代码贡献量 个人贡献分 ...