根据父节点和子节点下标关系保存x和y到根节点路径看是否有交点

二叉树

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

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            int x = sc.nextInt();
            int y = sc.nextInt();
            Set<Integer> set = new HashSet<>();
            while (x != 0) {
                set.add(x);
                x /= 2;
            }
            while (y != 0) {
                if (set.contains(y)) {
                    System.out.println(y);
                    break;
                }
                set.add(y);
                y /= 2;
            }
        }
    }
}

全部评论

相关推荐

06-28 22:48
已编辑
广东金融学院 Java
小浪_Coding:学院本+这俩项目不是buff叠满了嘛
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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