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实现 二叉树及遍历 二叉树概念 二叉 ...
随机推荐
- 论--如何通过代码解析plist文件创建对应的控制器,以及控制器中的控件
通过懒加载把最初的plist文件加载后,根据plist文件文件中的目标控制器进行跳转,根据加载的plist文件中的plist_name加载将要跳转进去的控制器界面的控件等等. 以上根据target_v ...
- Android Log图文详解
android.util.Log常用的方法有以下5个:Log.v() Log.d() Log.i() Log.w() 以及 Log.e() .根据首字母对应VERBOSE,DEBUG,INFO, WA ...
- G面经prepare: Sort String Based On Another
Given a sorting order string, sort the input string based on the given sorting order string. Ex sort ...
- ASPNET服务端控件练习(一个机试题)
简单记录: 模糊查询的select语句的拼写 public List<Model.Student> GetWhereStudent(string name, string sub, str ...
- SpringMvc:视图和视图解析器
请求处理方法执行完成后,最终返回一个ModelAndView对象,对于返回String,View或ModelMap等类型的处理方法,SpringMvc也会在内部将它们装配成一个ModelAndView ...
- centos dhcp网络设置
CentOS 网络设置修改 一.CentOS 修改IP地址 修改对应网卡的IP地址的配置文件# vi /etc/sysconfig/network-scripts/ifcfg-eth0 修改以下内 ...
- Android 测试Service的生命周期
package com.example.myapp4; import android.support.v7.app.ActionBarActivity; import android.content. ...
- 对于Mybatis在C#.Net中个人使用的总结(一) Mybatis 的结果映射
(图片中的文字上传之后就都看不清,我再图片的下边会用斜体字标清) 首先我在项目中使用Mybatis 是用XML完成映射的.至于XML这门语言,其实很简单的(对于入门来说,因为我是刚入门哈~),如果你还 ...
- 夺命雷公狗—angularjs—18—angularjs的事件
对于一款前端框架,提起事件,很容易让人联想到DOM事件,比如说鼠标点击以及页面滚动等.但是我们这里说的angular中的事件和DOM事件并不是一个东西. 事件的发布 我们可以通过 $emit() 以及 ...
- java 操作excel 文件
JAVA EXCEL API:是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件.使用该API非Windows操作系统也可以通过 ...