一、二叉树的后序遍历:

题目描述

给定一颗二叉树,要求输出二叉树的深度以及后序遍历二叉树得到的序列。本题假设二叉树的结点数不超过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. debug 使用lldb

    http://www.zddhub.com/memo/2015/12/20/lldb-golang-debug/ go build -gcflags "-N -l" -o test ...

  2. Java基础之一组有用的类——使用正则表达式搜索子字符串(TryRegex)

    控制台程序. 正则表达式只是一个字符串,描述了在其他字符串中搜索匹配的模式.但这不是被动地进行字符序列匹配,正则表达式其实是一个微型程序,用于一种特殊的计算机——状态机.状态机并不是真正的机器,而是软 ...

  3. 使用Java创建RESTful Web Service

    REST是REpresentational State Transfer的缩写(一般中文翻译为表述性状态转移).2000年Roy Fielding博士在他的博士论文“Architectural Sty ...

  4. CSS文档流与块级元素和内联元素

    CSS文档流与块级元素(block).内联元素(inline),之前翻阅不少书籍,看过不少文章, 看到所多的是零碎的CSS布局基本知识,比较表面.看过O'Reilly的<CSS权威指南>, ...

  5. Leetcode: Random Pick Index

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

  6. 如何在PHP里面连接数据库

    第一步   创造一个连接 $a = mysql_connect("localhost","root",""); 括号里面参数的意思: 1.l ...

  7. ruby的在ubuntu上的安装

    apt (Debian or Ubuntu) Debian GNU/Linux and Ubuntu use the apt package manager. You can use it like ...

  8. c++之路进阶——codevs1286(郁闷的出纳员)

    1286 郁闷的出纳员 2004年NOI全国竞赛  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master       题目描述 Description OIER公司 ...

  9. Hibernate的关系配置

    一对一: <hibernate-mapping> <class name="cn.jnit.bean.User" table="T_user" ...

  10. ZOJ 3645 BiliBili(高斯消元)

    Shirai Kuroko is a Senior One student. Almost everyone in Academy City have super powers, and Kuroko ...