一、二叉树的后序遍历:

题目描述

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

    转自: http://www.jb51.net/article/38936.htm 本篇文章是对MySQL内存的使用说明(全局缓存+线程缓存)进行了详细的分析介绍,需要的朋友参考下    首先我们来看 ...

  2. 微软bing搜索搜索源码,可以直接运行

    微软联合HackerRank一起研发了一项新功能:源代码搜索.能够直接搜索代码并且进行编译运行. 如果不做说明,这项功能看上去简直就是Visual Studio中源代码搜索插件的翻版,不过其并不需要本 ...

  3. iOS架构网址

    http://casatwy.com/iosying-yong-jia-gou-tan-kai-pian.html

  4. Swift动画编程指南-02 Swift动画是怎么炼成的

    上一节我们看了几个很棒的例子,我们不禁会想.他们是怎么设计的,怎么从一个空白的画布变成一个完整的,美丽的动画.这些动画是如何产生的,是哪些属性被改变了.我们还要认真思考的是,每一个步骤到底发生了什么. ...

  5. PostgreSQL Replication之第十一章 使用Skytools(5)

    11.5 关于walmgr 的介绍 walmgr 是一个简化基于文件事务日志传输的工具.早在过去的一些日子里(在9.0版本之前),使用walmgr来简化基本备份是很常见的.随着流复制的引入,情况有了一 ...

  6. (转)json+flexgrid+jbox组合运用页面刷新<jsp>

    插件效果 1.JSP页面 1 <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  7. JSP 使用框架frame

    JSP使用框架放在<head>标签里面.如果放在<body>标签里面,用Tomcat打开,是不会显示的.

  8. JQuery权限管理

    <title></title> <script src="JS/jquery-1.7.1.js"></script> <scr ...

  9. .NET: WPF DependencyProperty

    DependencyProperty and DependencyObject is the core of WPF data binding. We can use this two class t ...

  10. Windows平台上安装搭建iPhone/iPad的开发环境

    http://www.cnblogs.com/hanxianlong/archive/2015/09/20/4824227.html http://blog.csdn.net/yahohi/artic ...