题解 | #二叉树#

二叉树

https://www.nowcoder.com/practice/f74c7506538b44399f2849eba2f050b5

用BFS思想实现,不用递归

#include <iostream>
#include <queue>
using namespace std;

int main() {
    int n, m;
    while (scanf("%d %d", &m, &n) != EOF) {
        if(m==0 && n==0){
            break;
        }
        int total = 0;
        queue<int> myQueue;
        myQueue.push(m);
        while (!myQueue.empty()) {
            int current = myQueue.front();
            myQueue.pop();
            if(current>n){
                continue;
            }
            total++;
            myQueue.push(current*2);
            myQueue.push(current*2+1);
        }
        cout<<total<<endl;
    }
    return 0;
}

全部评论

相关推荐

湫湫湫不会java:先投着吧,大概率找不到实习,没实习的时候再加个项目,然后把个人评价和荣誉奖项删了,赶紧成为八股战神吧,没实习没学历,秋招机会估计不多,把握机会。或者说秋招时间去冲实习,春招冲offer,但是压力会比较大
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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