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 = 1005; 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 0 ; int x = Deepth(T->lchild); int y = Deepth(T->rchild); return max(x,y)+1 ; } 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 = 0 ; while( scanf("%d",&num)&&num!=-1)//建树 { if( flag )//头节点 { root = (BiTree)malloc(sizeof(BiNode)); root->data = num ; root->lchild = root->rchild = NULL ; if( root->data == 0 ) { 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 != 0 ) { 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 0; }
二、中序遍历二叉树
给定一颗二叉树,要求输出二叉树的深度以及中序遍历二叉树得到的序列。本题假设二叉树的结点数不超过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 = 1005; 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 0 ; int x = Deepth(T->lchild); int y = Deepth(T->rchild); return max(x,y)+1 ; } 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 = 0 ; while( scanf("%d",&num)&&num!=-1)//建树 { if( flag )//头节点 { root = (BiTree)malloc(sizeof(BiNode)); root->data = num ; root->lchild = root->rchild = NULL ; if( root->data == 0 ) { 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 != 0 ) { 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 0; }
三、前序遍历:
给定一颗二叉树,要求输出二叉树的深度以及先序遍历二叉树得到的序列。本题假设二叉树的结点数不超过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 = 1005; 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 0 ; int x = Deepth(T->lchild); int y = Deepth(T->rchild); return max(x,y)+1 ; } 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 = 0 ; while( scanf("%d",&num)&&num!=-1)//建树 { if( flag )//头节点 { root = (BiTree)malloc(sizeof(BiNode)); root->data = num ; root->lchild = root->rchild = NULL ; if( root->data == 0 ) { 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 != 0 ) { 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 0; }
树。。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了