当前位置:网站首页>二叉树(DAY 82)
二叉树(DAY 82)
2022-07-27 01:06:00 【张学恒】
1:题目
1
/ \
2 3
/ \ / \
4 5 6 7
/\ /\ /\ /\
... ...
如上图所示,由正整数 1,2,3,… 组成了一棵无限大的(满)二叉树。
从任意一个结点到根结点(编号是 1 的结点)都有一条唯一的路径,比如从 5 到根结点的路径是 (5,2,1),从 4 到根结点的路径是 (4,2,1),从根结点 1 到根结点的路径上只包含一个结点 1,因此路径就是 (1)。
对于两个结点 x 和 y,假设他们到根结点的路径分别是 (x1,x2,…,1) 和 (y1,y2,…,1),那么必然存在两个正整数 i 和 j,使得从 xi 和 yj 开始,有 xi=yj,xi+1=yj+1,xi+2=yj+2,…
现在的问题就是,给定 x 和 y,要求他们的公共父节点,即 xi(也就是 yj)。
输入格式
1≤x,y≤231−1
输入样例:
10 4
输出样例:
2
2:代码实现
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
int x, y;
cin >> x >> y;
while (x != y)
{
if (x > y) x /= 2;
else y /= 2;
}
cout << x << endl;
return 0;
}
边栏推荐
- Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions
- Zhang Ping, Alibaba cloud Solution Architect: system construction of cloud native digital safety production
- [二分查找简单题] LeetCode 35. 搜索插入位置,69. x 的平方根,367. 有效的完全平方数,441. 排列硬币
- [栈和队列简单题] LeetCode 232. 用栈实现队列,225. 用队列实现栈
- be based on. NETCORE development blog project starblog - (16) some new functions (monitoring / statistics / configuration / initialization)
- CS224W fall 课程 ---- 1.1 why Graphs ?
- 抖音服务器带宽有多大,才能供上亿人同时刷?
- 力扣(LeetCode)207. 课程表(2022.07.26)
- 全网最全的软件测试基础知识整理(新手入门必学)
- How to visit the latest version of burpsuite pro in vain
猜你喜欢

Attention should be paid to the first parameter of setTimeout

196. Delete duplicate email addresses

Redis四大特殊数据类型的学习和理解

次轮Okaleido Tiger即将登录Binance NFT,引发社区热议

Make ppt timeline

Hcip 13th day notes
全网最全的软件测试基础知识整理(新手入门必学)

be based on. NETCORE development blog project starblog - (16) some new functions (monitoring / statistics / configuration / initialization)

5、 MFC view windows and documents

LabVIEW中编程更改进程的优先级
随机推荐
be based on. NETCORE development blog project starblog - (16) some new functions (monitoring / statistics / configuration / initialization)
浅浅梳理一下双轴快排(DualPivotQuickSort)
Worth more than 100 million! The 86 version of "red boy" refuses to be a Daocheng Xueba. He is already a doctor of the Chinese Academy of Sciences and has 52 companies under his name
How big is the bandwidth of the Tiktok server for hundreds of millions of people to brush at the same time?
素因子分解--C(gcc)--PTA
力扣(LeetCode)207. 课程表(2022.07.26)
一个测试类了解BeanUtils.copyProperties
Analysis of [paper] pointlanenet papers
毕业2年转行软件测试获得12K+,不考研月薪过万的梦想实现了
CS224W fall 1.2 Applications of Graph ML
[binary search medium] leetcode 34. find the first and last positions of elements in the sorted array
Plato Farm通过LaaS协议Elephant Swap,为社区用户带来全新体验
Compile and use protobuf Library in vs2019
智能指针shared_ptr、unique_ptr、weak_ptr
Bulk copy baby upload prompt garbled, how to solve?
2513: 小勇学分数(公约数问题)
数据库红的表如何设计才能使性能更加优化
[SQL simple question] leetcode 627. change gender
Alibaba cloud technology expert Yang Zeqiang: Construction of observability on elastic computing cloud
LabVIEW中编程更改进程的优先级