当前位置:网站首页>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;
}
边栏推荐
- Repeated calls, messages, idempotent schemes, full collation
- 链表——142. 环形链表 II
- Literature record (part109) -- self representation based unsupervised exemplar selection in a union of subspaces
- cgo+gSoap+onvif学习总结:9、go和c进行socket通信进行onvif协议处理
- One week's wonderful content sharing (issue 13)
- [golang] before method of time type in golang
- 2022,软测人的平均薪资,看完我瞬间凉了...
- Grep actually uses ps/netstat/sort
- Code of login page
- Operational amplifier - Notes on rapid recovery [II] (application)
猜你喜欢

Cgo+gsoap+onvif learning summary: 9. Go and C conduct socket communication and onvif protocol processing

RetinaNet:Focal Loss for Dense Object Detection

iMeta观点 | 短读长扩增子测序是否适用于微生物组功能的预测?
![[markdown grammar advanced] make your blog more exciting (IV: set font style and color comparison table)](/img/a5/c92e0404c6a970a62595bc7a3b68cd.gif)
[markdown grammar advanced] make your blog more exciting (IV: set font style and color comparison table)

Nodejs ctf 基础

DevOps及DevOps常用的工具介绍

ctfshow ThinkPHP专题 1

Linked list - Sword finger offer interview question 02.07. linked list intersection

Lanqiao cup provincial training camp - stack and recursion

生信周刊第37期
随机推荐
iMeta观点 | 短读长扩增子测序是否适用于微生物组功能的预测?
[golang] deletion and emptying of map elements in golang
Leetcode 257. 二叉树的所有路径
SSH跨平台终端工具tabby推荐
Basic syntax of MySQL DDL and DML and DQL
Ask n! How many zeros are there behind
链表——142. 环形链表 II
Tensor and numpy convert "suggested collection" to each other
这才是开发者神器正确的打开方式!
This is the right way for developers to open artifact!
Exceptions about configuring Postgres parameters
Cgo+gsoap+onvif learning summary: 9. Go and C conduct socket communication and onvif protocol processing
Grep actually uses ps/netstat/sort
6k+ star, a deep learning code base for Xiaobai! One line of code implements all attention mechanisms!
Hash - 242. valid alphabetic ectopic words
Two important laws about parallelism
How to access the code of online customer service system to your website
6k+ star,面向小白的深度学习代码库!一行代码实现所有Attention机制!
[QNX hypervisor 2.2 user manual]9.2 CmdLine
运算放大器 —— 快速复苏笔记[壹](参数篇)