题解 | 二叉树
二叉树
https://www.nowcoder.com/practice/5b80ab166efa4551844657603227caeb
#include <iostream>
#include<vector>
using namespace std;
int main() {
int x, y;
while (cin >> x >> y) {
vector<int> path1;
while (x != 0) {
path1.push_back(x);
x /= 2;
}
vector<int> path2;
while (y != 0) {
path2.push_back(y);
y /= 2;
}
int i = 0, j = 0;
while (path1.size() - i != path2.size() - j) {
//公共祖先还是要用爬山法
if (path1.size() - i > path2.size() - j) {
i++;
}
else if (path1.size() - i < path2.size() - j) {
j++;
}
}
for (; i < path1.size() && j < path2.size(); i++, j++) {
if (path1[i] == path2[j]) {
cout << path1[i]<<endl;
break;
}
}
}
}
// 64 位输出请用 printf("%lld")