题解 | 二叉树

二叉树

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

from collections import defaultdict,deque

def same_father(a:int,b:int):
    temp1=a
    temp2=b
    while temp1!=temp2:
        while temp1>temp2 :
            if temp1%2==1:
                temp1=(temp1-1)//2
            else:
                temp1=temp1//2
        while temp2 > temp1 :
                if temp2%2==1:
                    temp2=(temp2-1)//2
                else:
                    temp2=temp2//2
    if temp1>1:
        return temp1
    else:
        return 1


if __name__=="__main__":
    while True:
        try:
            string=input().split()
            a,b=int(string[0]),int(string[1])
            
            print(same_father(a,b))
        except EOFError:
            break

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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