一、二叉树的后序遍历:

题目描述

给定一颗二叉树,要求输出二叉树的深度以及后序遍历二叉树得到的序列。本题假设二叉树的结点数不超过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题目————二叉树的遍历的更多相关文章

  1. ACM题目————二叉树最大宽度和高度

    http://codevs.cn/problem/1501/   题目描述 Description 给出一个二叉树,输出它的最大宽度和高度. 输入描述 Input Description 第一行一个整 ...

  2. [PTA] 数据结构与算法题目集 6-8 求二叉树高度 & 6-9 二叉树的遍历

    6.8 二叉树高度 int GetHeight(BinTree BT) { if (BT == NULL) return 0; int leftH = GetHeight(BT->Left); ...

  3. ACM 重建二叉树

    重建二叉树 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 题目很简单,给你一棵二叉树的后序和中序序列,求出它的前序序列(So easy!).   输入 输入有多组数 ...

  4. C++版 - 剑指Offer 面试题39:二叉树的深度(高度)(二叉树深度优先遍历dfs的应用) 题解

    剑指Offer 面试题39:二叉树的深度(高度) 题目:输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.例如:输入二叉树 ...

  5. DS二叉树--层次遍历

    题目描述 层次遍历二叉树,是从根结点开始遍历,按层次次序“自上而下,从左至右”访问树中的各结点. 建树方法采用“先序遍历+空树用0表示”的方法 要求:采用队列对象实现,函数框架如下: 输入 第一行输入 ...

  6. 二叉树的遍历 &【NOIP2001普及组】& 洛谷 P1030 求先序排列

    题目链接 https://www.luogu.org/problemnew/show/P1030 模板题 先讲一下二叉树的遍历 二叉树的遍历 分类 性质 求法 分为三类: 先序遍历(PreOrder) ...

  7. 【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】

    [107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a ...

  8. [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 ...

  9. 【数据结构】二叉树的遍历(前、中、后序及层次遍历)及leetcode107题python实现

    文章目录 二叉树及遍历 二叉树概念 二叉树的遍历及python实现 二叉树的遍历 python实现 leetcode107题python实现 题目描述 python实现 二叉树及遍历 二叉树概念 二叉 ...

随机推荐

  1. iOS 在一个应用程序中调另一个应用程序

    在A应用程序中调用B应用程序 1. 首先在B应用程序中生成URL 1)点击targets文件 2)点击Info 3)生成URL ①在Info.plist文件中点击+(新添加一项) ②在Info.pli ...

  2. ios常见细节问题-删掉main.storyboard程序启动屏幕变黑-崩溃

    删掉程序默认的main.storyboard文件后,程序启动崩溃 如图所示.原因是删掉main.storyboard文件后没有在info.plist文件里面设置 删掉main.storyboard后程 ...

  3. ads

    (3) Make可以编译整个工程,并生成映像文件.在ADS中,ARM提供了三种映像文件,      1. Debug:      使用本生成目标生成的映像文件中包含了所有的调试信息,用于开发过程中使用 ...

  4. Leetcode: Frog Jump

    A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...

  5. mvc3在window 7 iis7下以及 xp iis 5.1下的部署 ,asp.net MVC3无法打开项目文件E:/我们的项目/Project/HeatingMIS.Web/HeatingMIS.Web.csproj”。此安装不支持该项目类型。

    今天,小白来总结一下我在is上部署mvc3 .net 网站的时候的过程和遇到的问题. 其实,mvc的网站的部署跟平常的网站的部署都是一样的,只是下面有一些需要注意的地方. 1.应用程序池采用集成模式( ...

  6. C++之路起航——标准模板库(queue)

    queue: FIFO队列:先进先出队列. 优先队列:对队列中的元素按优先级的大小输出. 定义: FIFO队列: queue<数据类性>变量名. 优先队列:priority_queue&l ...

  7. 是否可以继承String类?

    是否可以继承String类? String类是final类故不可以继承

  8. 在Visual Studio 2013/2015上使用C#开发Android/IOS安装包和操作步骤

    Xamarin 配置手册和离线包下载 http://pan.baidu.com/s/1eQ3qw8a 具体操作: 安装前提条件 1. 安装Visual Studio 2013,安装过程省略,我这里安装 ...

  9. springmvcの神总结のreadme

    ********李守宏springmvc******** 3.== --\springmvc一个controller实现多个方法 ----\继承MultiActionController ----\配 ...

  10. android 应用架构随笔二(定义BaseApplication并配置Application)

    定义BaseApplication并配置Application import android.app.Application; import android.os.Handler; /** * * = ...