题解 | #二叉排序树#

二叉排序树

https://www.nowcoder.com/practice/30a0153649304645935c949df7599602

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

typedef struct Binode {
    int data;
    struct Binode* lchild;
    struct Binode* rchild;
    Binode(int x): data(x), lchild(NULL), rchild(NULL) {}
};
void insertTree(int x, Binode* b) {
    if (x > b->data) {
        if (b->rchild == NULL) {
            Binode* node = new Binode(x);
            b->rchild = node;
            cout << b->data;

        } else {
            Binode* temporary = b;
            insertTree(x, temporary->rchild);
        }

    } else {
        if (b->lchild == NULL) {
            Binode* node = new Binode(x);
            b->lchild = node;
            cout << b->data;

        } else {

            insertTree(x, b->lchild);
        }
    }

}
int main() {
    int num0;
    vector<int> value;
    while (cin >> num0) {
        int num = num0;
        int x;
        cin >> x;
        Binode* node = new Binode(x);
        cout << -1 << "\n";
        Binode* root = node; //保存根节点
        while (num - 1 > 0) {
            cin >> x;
            insertTree(x, node);
            node = root;
//            cout<<node->data;
            num--;
            cout << "\n";
        }

    }
    return 0;
}

全部评论

相关推荐

08-20 15:28
门头沟学院 Java
既然要挂我,就不要一直邀请我投递
投递字节跳动等公司10个岗位
点赞 评论 收藏
分享
06-26 15:33
青岛工学院 Java
积极的秋田犬要冲国企:他现在邀请我明天面试
点赞 评论 收藏
分享
虾皮笔试过了80%给我挂了。。。。这要多少才能过呀
投递深圳虾皮信息科技有限公司等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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