当前位置:网站首页>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;
}
边栏推荐
- Leetcode 257. 二叉树的所有路径
- Introduction to Devops and common Devops tools
- 什么是云原生,云原生技术为什么这么火?
- 【反序列化漏洞-01】序列化与反序列化简介
- [deserialization vulnerability-02] principle test and magic method summary of PHP deserialization vulnerability
- 视频回放 | 如何成为一名优秀的地学和生态学领域的国际期刊审稿人?
- Types and history of bugs in it circle
- 运算放大器 —— 快速复苏笔记[贰](应用篇)
- Import the data in MariaDB into columnstore
- HCIP MGRE实验 第三天
猜你喜欢

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

08.01 adjacency matrix

2 万字详解,吃透 ES!

安装jmeter
![MOS管 —— 快速复苏应用笔记(壹)[原理篇]](/img/a1/8427c9b1d0ea0cecce820816510045.png)
MOS管 —— 快速复苏应用笔记(壹)[原理篇]

IT圈中的Bug的类型与历史

Directional crawling Taobao product name and price (teacher Songtian)

源码分析Sentry用户行为记录实现过程

cgo+gSoap+onvif学习总结:9、go和c进行socket通信进行onvif协议处理

Easy to use example
随机推荐
Shell script "< < EOF" my purpose and problems
Shell Scripting tips
生信周刊第37期
String - Sword finger offer 05. replace spaces
One week's wonderful content sharing (issue 13)
链表——142. 环形链表 II
什么是云原生,云原生技术为什么这么火?
The difference between YPbPr and YCbCr
黑马瑞吉外卖之员工信息分页查询
强引用、软引用、弱引用、虚引用有什么区别?
Fiddler packet capture tool summary
Reprint of illustrations in nature, issue 3 - area map (part2-100)
Hash - 15. Sum of three numbers
SSH跨平台终端工具tabby推荐
IT圈中的Bug的类型与历史
字符串——344.反转字符串
Use of getchar
Jmeter-While控制器
Paging query of employee information of black maredge takeout
字符串——541. 反转字符串 II