一、二叉樹的后序遍歷:
給定一顆二叉樹,要求輸出二叉樹的深度以及后序遍歷二叉樹得到的序列。本題假設二叉樹的結點數不超過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; }
樹。。