当前位置:网站首页>L2-011 play with binary tree
L2-011 play with binary tree
2022-07-24 11:42:00 【Cod_ ing】
Topic link
Similar topics
L2-006 Tree traversal
step :
1、 Establish binary tree according to preorder traversal and inorder traversal
2、 Recursively flip left and right subtrees
3、 Use the first in first out property of the queue to realize hierarchical traversal ( breadth-first )
#include<iostream>
#include<stdlib.h>
#include<vector>
#include<queue>
using namespace std;
int A[35];
int B[35];
int N;
typedef struct TNode {
struct TNode* left;
struct TNode* right;
int data;
}*Tree;
Tree BuildTree(int X[],int Y[],int n) {
// Build a binary tree
if (n <= 0 ) return NULL;
int i = 0;
while (X[i] != Y[0])i++;
//cout << n << endl;
Tree p = (Tree)malloc(sizeof(struct TNode));
p->data = X[i];
//cout << "left" << endl;
p->left = BuildTree(X, &Y[1], i);
//cout << "right" << endl;
p->right = BuildTree(&X [ i + 1], &Y [ i + 1], n - i - 1);
return p;
}
void Reverse(Tree& T) {
// Flip left and right subtrees
if (T == NULL) return;
Reverse(T->left);
Reverse(T->right);
TNode* temp = T->left;
T->left = T->right;
T->right = temp;
}
vector<int> Level(Tree T) {
// Level traversal
queue<TNode*> Q;
vector<int> ans;
Q.push(T);
while (!Q.empty()) {
TNode* p = Q.front();
Q.pop();
if (p->left != NULL) Q.push(p->left);
if (p->right != NULL)Q.push(p->right);
ans.push_back(p->data);
}
return ans;
}
int main() {
cin >> N;
for (int i = 0; i < N; i++) cin >> A[i];
for (int i = 0; i < N; i++) cin >> B[i];
Tree root = BuildTree(A, B, N);
Reverse(root);
vector<int> ans = Level(root);
for (int i = 0; i < N; i++) {
cout << ans[i];
if (i != N - 1) cout << " ";
}
return 0;
}
边栏推荐
- String - Sword finger offer 05. replace spaces
- PDF处理还收费?不可能!
- Detailed explanation of stat function
- 运算放大器 —— 快速复苏笔记[贰](应用篇)
- Is there any charge for PDF processing? impossible!
- Reprint of illustrations in nature, issue 3 - area map (part2-100)
- Judge whether a group of cards can become shunzi (the size of the king is 14,15)
- [golang] golang implements the string interception function substr
- Nacos permissions and databases
- L1-049 天梯赛座位分配
猜你喜欢

20000 words detailed explanation, thoroughly understand es!

Text message verification of web crawler

[TA frost wolf umay - "hundred people plan] Figure 3.3 surface subdivision and geometric shader large-scale grass rendering

08.01 adjacency matrix

Stream stream
![08 [AIO programming]](/img/a6/156cb97e653190c76f22c88b758fef.png)
08 [AIO programming]

Shell script "< < EOF" my purpose and problems

Cgo+gsoap+onvif learning summary: 9. Go and C conduct socket communication and onvif protocol processing
什么是云原生,云原生技术为什么这么火?

HCIP MGRE实验 第三天
随机推荐
Basic syntax of MySQL DDL and DML and DQL
Differences between JS map and foreach
Paging query of employee information of black maredge takeout
Database operation through shell script
Imeta view | is short reading long amplicon sequencing applicable to the prediction of microbiome function?
cgo+gSoap+onvif学习总结:9、go和c进行socket通信进行onvif协议处理
Hcip OSPF interface network type experiment day 4
哈希——242.有效的字母异位词
源码分析Sentry用户行为记录实现过程
Hash - 15. Sum of three numbers
The art of management - driving software R & D efficiency through leadership
离散分布常用公式及应用场景
Hash - 349. Intersection of two arrays
L1-049 天梯赛座位分配
1184. 公交站间的距离 : 简单模拟题
Collision, removal and cleaning
MOS tube - Notes on rapid recovery application (I) [principle]
字符串——541. 反转字符串 II
NFT digital collection system construction - app development
一周精彩内容分享(第13期)