我是傻逼
题目:
给一颗节点数为n的完全二叉树,对于每个“左斜节点集”做一次循环上移k单位的操作,问移动后这棵树的层先遍历。
例如:
n = 7, k = 1:
移动前:
移动后:
解释:
一共四个“左斜节点集”,分别是(有序集合,顺序从上到下):
{1,2,4}
{5}
{3,6}
{7}
每个集合都循环上移1个单位
变成:
{2,4,1}
{5}
{6,3}
{7}
输入:
第一行输入一个整数t,表示测试次数
接下来t行,每行输入两个整数,分别是n和k
输出:
移动后该树的层先遍历。
样例输入:
7 1
样例输出
2 4 6 1 5 3 7
我是傻逼!我为什么会傻到要去模拟这个过程!这样的“左斜节点集”父子关系不就是两倍吗?!直接就能算出来,模拟的写法复杂且极易出错,费时费力费内存,写到考完后十几分钟才写出来。
模拟做法
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <queue>
#include <map>
using namespace std;
struct Info{
vector<int> *vec_p;
int idx;
Info(){}
Info(vector<int> *v, int i) {
vec_p = v;
idx = i;
}
};
struct Node{
int value;
Node *left;
Node *right;
Node(int v):value(v), left(NULL), right(NULL){}
};
Node *create_n_tree(int n) {
Node *root = new Node(1);
queue<Node*> q;
q.push(root);
while(q.size()) {
Node *curr = q.front(); q.pop();
int v = curr->value;
if ( v*2 <= n ) {
curr->left = new Node(v*2);
q.push(curr->left);
}
if ( v*2+1 <= n ) {
curr->right = new Node(v*2+1);
q.push(curr->right);
}
}
return root;
}
void print_tree(Node *root, int deep = 1) {
if (root) {
print_tree(root->left, deep+1);
for (int i = 0; i < deep; ++i){
cout << "\t";
}
cout << root->value << endl;
print_tree(root->right, deep+1);
}
}
int main(int argc, char const *argv[]){
// print_tree(create_n_tree(7));
int t;
cin>>t;
while(t--) {
int n,k;
cin>>n>>k;
Node *root = create_n_tree(n);
map<Node*, Info> the_mp;
vector<Node *> v;
vector<bool> l_r;
l_r.push_back(1);
v.push_back(root);
map<Node*, bool> vis;
int win_left = -1;
int win_right = 0; // [left+1, right]
while(v.size()) {
Node *curr = *(v.end()-1);
vis[curr] = 1;
// cout << "curr->value = " << curr->value << endl; //log
if (curr->left==NULL && curr->right==NULL) {
// cout << "l_r.size() = " << l_r.size() << endl; //log
// for ( auto i : l_r) {
// cout << i << " ";
// }
// cout << endl;
auto p = v.end()-1;
// cout << "(*p)->value = " << (*p)->value << endl; //log
vector<int> * vp = new vector<int>();
vector<int> vvvv;
int idx = 0;
the_mp[*p] = Info(vp, idx++);
vp->push_back((*p)->value);
p--;
for ( int i=l_r.size()-1; i>=0&&l_r[i]==0; i--) {
// cout << "i = " << i << endl; //log
// cout << "(*p)->value = " << (*p)->value << endl; //log
the_mp[*p] = Info(vp, idx++);
vp->push_back((*p)->value);
p--;
}
// cout << endl;
}
// cout << "v.size() = " << v.size() << endl; //log
if (curr->left && !vis[curr->left] ) {
win_right = v.size()-1;
v.push_back(curr->left);
l_r.push_back(0);
} else if ( curr->right && !vis[curr->right] ) {
v.push_back(curr->right);
win_left = v.size()-1;
l_r.push_back(1);
} else {
v.pop_back();
l_r.pop_back();
}
}
/*
for( auto pair : the_mp ) {
cout << "pair.first->value = " << (pair.first)->value << endl; //log
cout << "pair.second->idx = " << (pair.second).idx << endl; //log
cout << "pair.second->vec_p = " << (pair.second).vec_p << " : "; //log
cout << "[";
for ( auto item : *(pair.second.vec_p) ) {
cout << item << ' ';
}
cout << "]" << endl;
}
*/
queue<Node*> q;
q.push(root);
while(q.size()) {
Node *curr = q.front(); q.pop();
if ( curr != NULL) {
// cout << curr->value << " ";
int idx = the_mp[curr].idx;
int len = the_mp[curr].vec_p->size();
cout << (*(the_mp[curr].vec_p))[(idx-k%len+len)%len] << " ";
q.push(curr->left);
q.push(curr->right);
}
}
cout << endl;
}
return 0;
}
利用左孩子等于父节点值*2做:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <queue>
#include <map>
using namespace std;
int main(int argc, char const *argv[]){
int t;
cin>>t;
while(t--) {
int n,k;
cin>>n>>k;
queue<int> q;
q.push(1);
while(q.size()) {
int curr = q.front(); q.pop();
int v = curr;
for (int i = 0; i < k; ++i){
v *= 2;
if (v>n) {
while(v%2==0){ // 回到这条“左链”的最上
v /= 2;
}
}
}
cout << v << " ";
if (curr*2<n) {
q.push(curr*2); // left
q.push(curr*2+1); // right
}
}
cout << "" << endl;
}
return 0;
}
所以说,我是傻逼!!!