题解 | 最近公共祖先
最近公共祖先
https://www.nowcoder.com/practice/70e00e490b454006976c1fdf47f155d9
class LCA {
public:
    int getLCA(int a, int b) {
        // write code here
        if(a>b) return getLCA(b, a);
        if(a==b) return a;
        if(b/2 > a) return getLCA(a, b/2);
        if(b/2 < a) return getLCA(a/2, b/2);
        return a;
    }
};


查看5道真题和解析