二叉树1 (猴子下山)

描述

有一颗二叉树,最大深度为D,且所有叶子的深度都相同。所有结点从左到右从上到下的编号为1,2,3,…,2的D次方减1(满二叉树)。在结点1处放一个小猴子,它会往下跑。每个内结点上都有一个开关,初始全部关闭,当每次有小猴子跑到一个开关上时,它的状态都会改变,当到达一个内结点时,如果开关关闭,小猴子往左走,否则往右走,直到走到叶子结点。

一些小猴子从结点1处开始往下跑,最后一个小猴儿会跑到哪里呢?

 输入

输入二叉树叶子的深度D,和小猴子数目I,假设I不超过整棵树的叶子个数,D<=20.最终以 0 0 结尾

输出

输出第I个小猴子所在的叶子编号。

样例输入

4 2

3 4

0 0

样例输出

12

7

#include"iostream"
#include"queue"
#include"cmath"
using namespace std;
typedef int element;

static bool *change;            //记录结点开关
static int count1;                //记录猴子到达个数
class Tree{
private:
    element data;
    Tree *right;
    Tree *left;
public:

    Tree(){}
    Tree(element data){
        this->data = data;
        right = NULL;
        left = NULL;
    }
    void createSQTree(Tree* &t,int n){
        queue<Tree*> q;
        int i = 0;
        t = new Tree(++i);
        Tree *p = t;
        q.push(p);
        while(i != n){
            p = q.front();
            p->left = new Tree(++i);
            q.push(p->left);
            if(i == n){
                break;
            }
            p->right = new Tree(++i);
            q.push(p->right);
            q.pop();
        }
    }
    void destroy(){
        if(this){
            left->destroy();
            right->destroy();    
            delete this;
        }
    }
    void showx(Tree* t,int momkey){
        if(t){
            if(t->left == NULL && t->right == NULL && count1 == momkey - 1){
                cout<<t->data<<endl;
                return ;
            }
            if(change[t->data]){
                change[t->data] = false;
                showx(t->left,momkey);
            }
            else{
                change[t->data] =true;
                showx(t->right,momkey);
            }
        }
        else{
            if(++count1 == momkey){
                return ;
            }
            showx(this,momkey);
        }
    }
};

int main(){
    Tree *t = NULL;
    while(true){
        int d;                //深度
        int momkey;            //猴子个数
        cin>>d>>momkey;
        if(d==0&&momkey==0){
            break;
        }
        count1 = 0;
        int num = pow(2,d) - 1;    //结点个数
        change = new bool[num + 1];
        for(int i = 0;i < num;i++){
            change[i] = true;
        }    
        t->createSQTree(t, num);
        t->showx(t,momkey);
        t->destroy();
        delete[] change;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/oleolema/p/9028437.html