#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<sstream>
using namespace std;
#define L 101
typedef struct node;
typedef node *tree; //tree => &node;tree本身就代表地址了
struct node{
bool flag = false;
int data;
tree left,right;
int depth=1;
};
const int D=4,I=2;
tree a[L]; //定义line
int head=0,tail=1;
tree bt;
tree root;
//int i=1; //确定data
int id;
int main()
{
//1.建树
//满二叉树可以通过 父节点N -> 子节点N
root = new node;
root->data=1;
a[head] = root;
while(head < tail){
head++;
if(a[head-1]->depth<4){
//定义左子树
bt=new node; //bt仅保存一段地址
bt->depth = a[head-1]->depth+1;
bt->data = a[head-1]->data * 2;
a[head-1]->left = bt;
a[tail++] = bt;
//定义左子树
bt=new node; //开辟一段新地址,不会影响到以前的
bt->depth = a[head-1]->depth+1;
bt->data = a[head-1]->data * 2+1;
a[head-1]->right = bt;
a[tail++] = bt;
}else{
head = tail; //停止了
}
}
//2.遍历树
for(int i=1;i<=I;i++){
bt=root; //指向根节点
while(bt->depth<D){
if(bt->flag){ //向右走
bt->flag = !bt->flag;
bt = bt->right;
}else{ //向左走
bt->flag = !bt->flag;
bt = bt->left;
}
}
id = bt->data; //可覆盖;
}
cout << id;
return 0;
}
//cout << "输入:"
练习1 - 小球
最新推荐文章于 2024-01-07 12:23:30 发布