当前位置:网站首页>7.19 二叉树
7.19 二叉树
2022-07-22 09:52:00 【Zhou Xuanhong】
题目链接
做题情况
7min AC
代码及题解
本题比较简单,直接观察二叉树特性就可得出。
在数据结构这门课上也学过,给一个满二叉树,就可以通过孩子的序号得到其双亲的序号,及(双亲序号=孩子序号/2)
因此本题只需要将两个孩子的序号的所有祖先写出来,然后遍历查询最近的相同的那个就可以
#include<iostream>
using namespace std;
const int N=50;
long long x,y;
long long a[N],b[N];
int main(){
cin>>x>>y;
int num1=1;
int num2=1;
a[1]=x;
b[1]=y;
while(x!=0){
a[++num1]=x/2;
x/=2;
}
while(y!=0){
b[++num2]=y/2;
y/=2;
}
for(int i=1;i<=num1;i++){
for(int j=1;j<=num2;j++){
if(a[i]==b[j]){
cout<<a[i]<<endl;
exit(0);
}
}
}
return 0;
}
后记
去冠宏家吃饭咯
边栏推荐
猜你喜欢
pytest接口自动化测试框架 | 为什么要做pytest插件的二次开发
助力品牌洞察——消费者情绪行为分析
Grafana panel - about conversion
Several trends in the future development of software industry
MySQL optimization enforces the use of indexes
博士申请 | 香港大学倪卓娴教授招收物联网与机器学习方向全奖博士生
用指针遍历数组
【数据挖掘工程师-笔试】2022年SHEIN 公司
The problem that double type cannot be accurately calculated
Grafana panel - modify visual text and background colors
随机推荐
工业路由器油田无线监控
Execute function now
MySQL optimization enforces the use of indexes
Rongyun handles political affairs: "small grid" can also achieve "big governance"
LeetCode 每日一题 2021/12/6-2021/12/12
Methods of wrapping classes and strings
Force deduction solution summary 814 binary tree pruning
Go 中的基础库Time时间处理
Leetcode daily question 2021/11/29-2021/12/5
Force deduction solution summary 1200 minimum absolute difference
Force deduction solution summary 735 planetary collision
Prototype object
Leetcode daily question 2021/12/27-2022/1/2
Several trends in the future development of software industry
LeetCode 每日一题 2021/11/22-2021/11/28
Introduction to functions
LeetCode 每日一题 2022/1/31-2022/2/6
SQL 查询语句性能优化
An analysis of the CPU surge of an RFID tag management system in.Net
Character set and character encoding