mirror of
https://github.com/hao14293/2021-Postgraduate-408.git
synced 2026-11-04 14:54:00 +08:00
Create L2-011 玩转二叉树 (25 分).cpp
This commit is contained in:
40
GPLT/CPP/L2-011 玩转二叉树 (25 分).cpp
Normal file
40
GPLT/CPP/L2-011 玩转二叉树 (25 分).cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
vector<int> InOrder, PreOrder, Level(10000, -1);
|
||||
// 递归层次遍历
|
||||
void getLevel(int root, int start, int end, int index);
|
||||
|
||||
int main(){
|
||||
int n, cnt = 0;
|
||||
cin >> n;
|
||||
InOrder.resize(n);
|
||||
PreOrder.resize(n);
|
||||
//Level.resize(n);
|
||||
//vector<int> InOrder(n), PreOrder(n), Level(n);
|
||||
for(int i = 0; i < n; i++)
|
||||
cin >> InOrder[i];
|
||||
for(int i = 0; i < n; i++)
|
||||
cin >> PreOrder[i];
|
||||
getLevel(0, 0, n-1, 0);
|
||||
for(int i = 0; i < Level.size(); i++){
|
||||
if(Level[i] != -1 && cnt != n - 1){
|
||||
cout << Level[i] << " ";
|
||||
cnt++;
|
||||
}else if(Level[i] != -1){
|
||||
cout << Level[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void getLevel(int root, int start, int end, int index){
|
||||
if(start > end) return ;
|
||||
int i = start;
|
||||
while(i < end && InOrder[i] != PreOrder[root]) i++;
|
||||
Level[index] = PreOrder[root];
|
||||
getLevel(root + 1, start, i - 1, 2 * index + 2);
|
||||
getLevel(root + 1 + i - start, i + 1, end, 2 * index + 1);
|
||||
}
|
||||
Reference in New Issue
Block a user