Codeforces Round #287 (Div. 2) C

507C - Guess Your Way Out!


        题意:遍历一棵满二叉树,遍历方法是左右左右递归地遍历。树高度为h,最下层第n个节点是出口,问访问到出口前要访问多少个节点。

        思路:按题目要求的顺序去递归遍历,判断出口在以当前节点为根的左子树还是右子树中,如果是在后访问的子树中,直接加上2^h,因为根加上另一颗子树刚好有2^h个节点,再往下递归。


#include <iostream>
#include <stdio.h>
#include <string>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <string.h>
#include <algorithm>
#include <math.h>

using namespace std;

#define ll long long

ll pow2n[55];

//falg=0:先访问左孩子 //flag=1:先访问右孩子 
//h表示当前子树高度 n表示出口是第几个叶子 
ll fun(ll h,bool flag,ll n){	
	if(h==0)return 1LL; 
	if(flag==0){
		if(n>pow2n[h-1]){
			return fun(h-1,0,n-pow2n[h-1])+pow2n[h];
		}else{
			return fun(h-1,1,n)+1;
		}
	}else{
		if(n>pow2n[h-1]){
			return fun(h-1,0,n-pow2n[h-1])+1;
		}else{
			return fun(h-1,1,n)+pow2n[h];
		}
	}
	
}

int main(){
	//计算2的幂 
	pow2n[0]=1;
	for(int i=1;i<=50;i++){
		pow2n[i]=pow2n[i-1]<<1;
	}
	
	ll h,n;
	cin>>h>>n;
	
	ll ans=fun(h,0,n);
	cout<<ans-1<<endl;
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值