public class LCA {
public int getLCA(int a, int b) {
// <<左移 >>右移
if(a == b) {
return a;
}
while(a != b) {
a = a > b ? a>>1 : a;
b = b > a ? b>>1 : b;
}
return a;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
int j = sc.nextInt();
LCA c = new LCA();
System.out.println(c.getLCA(i,j));
}
}
牛客寻找最近公共祖先
最新推荐文章于 2022-10-07 14:16:41 发布