题解 | #二叉树#

二叉树

http://www.nowcoder.com/practice/5b80ab166efa4551844657603227caeb

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;


//找路径
void f(int x,vector<int>&v) {
	while (x != 0) {
		v.push_back(x);
		x /= 2;
	}
}
int main() {

	int x, y;
	while (cin >> x >>  y) {
		vector<int>pathx;
		vector<int>pathy;
		f(x,pathx);
		f(y,pathy);
		reverse(pathx.begin(), pathx.end());
		reverse(pathy.begin(), pathy.end());

		int comfather;
		int i = 0;
		for (; i < pathx.size()&&i<pathy.size(); i++) {
			if (pathx[i] != pathy[i])break;
		}
		cout << pathx[i-1] << endl;
	}
	

	return 0;
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务