当前位置:网站首页>L2-011 玩转二叉树
L2-011 玩转二叉树
2022-07-24 11:39:00 【Cod_ing】
题目链接
相似题目
L2-006 树的遍历
步骤:
1、根据前序遍历和中序遍历建立二叉树
2、递归翻转左右子树
3、使用队列的先进先出性质实现层次遍历(广度优先)
#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) {
//建立二叉树
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) {
//翻转左右子树
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) {
//层次遍历
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 112. path sum
- Idea runs the wordcount program (detailed steps)
- JVM visualvm: multi hop fault handling tool
- Online customer service chat system source code_ Beautiful and powerful golang kernel development_ Binary operation fool installation_ Construction tutorial attached
- Hash - 202. Happy number
- Imeta view | is short reading long amplicon sequencing applicable to the prediction of microbiome function?
- 2 万字详解,吃透 ES!
- Collision, removal and cleaning
- Reprint of illustrations in nature, issue 3 - area map (part2-100)
- [deserialization vulnerability-02] principle test and magic method summary of PHP deserialization vulnerability
猜你喜欢

Lanqiao cup provincial training camp - commonly used STL

Imeta view | is short reading long amplicon sequencing applicable to the prediction of microbiome function?

One week's wonderful content sharing (issue 13)

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

黑马瑞吉外卖之员工信息分页查询

使用Prometheus+Grafana实时监控服务器性能

Stream stream

Remember to optimize my personal blog once

Cgo+gsoap+onvif learning summary: 9. Go and C conduct socket communication and onvif protocol processing
![Operational amplifier - Notes on rapid recovery [II] (application)](/img/fd/e12f43e23e6ec76c2b44ce7813e204.png)
Operational amplifier - Notes on rapid recovery [II] (application)
随机推荐
Robot framework official tutorial (I) getting started
MOS管 —— 快速复苏应用笔记(壹)[原理篇]
Collision, removal and cleaning
Operational amplifier - Notes on rapid recovery [II] (application)
Reprint of illustrations in nature, issue 3 - area map (part2-100)
Sorting out the ideas of data processing received by TCP server, and the note of select: invalid argument error
String - Sword finger offer 05. replace spaces
Video playback | how to become an excellent reviewer of international journals in the field of Geoscience and ecology?
Ask n! How many zeros are there behind
Grep actually uses ps/netstat/sort
Install MariaDB columnstore (version 10.3)
The third day of hcip mGRE experiment
【10】 Teamwork and cross team collaboration
In BS4.String and Difference between text
[TA frost wolf umay - "hundred people plan] Figure 3.3 surface subdivision and geometric shader large-scale grass rendering
SSH跨平台终端工具tabby推荐
Share the typora tool
What is the difference between strong reference, soft reference, weak reference and virtual reference?
Two important laws about parallelism
[golang] golang implements the string interception function substr