题解 | #二叉排序树#

二叉排序树

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

#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* left;
    Node* right;
    Node(int a) : data(a), left(NULL), right(NULL){}
};

// root->a
// a
Node* insert(Node* root, int x, int father){
    if(root == NULL){
        root = new Node(x);
        cout << father << endl;
    }
    else if (x < root->data) {
        root->left = insert(root->left, x, root->data);
    }
    else {
        root->right = insert(root->right, x, root->data);
    }

    return root;
}

int main() {
    int n, x;
    while (cin >> n) {
        Node* root = NULL;
        while (n --) {
            cin >> x;
            root = insert(root, x, -1);
        }    
    }

    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
09-23 11:48
点赞 评论 收藏
分享
wish233:只是说使用xxx实现什么什么,没有原因,没有数据量化,就没有亮点。比如说第一个项目为什么要使用MongoDB?相比MySQL解决了什么问题,有什么好处?还有第二个项目用RestTemplate,既然引入了SpringCloud,你也写了自己的专业技能是能用里面的组件,那你为什么不用feign?还有就是你用的这些框架写上去的格式尽量统一一下,一会写spring,一会又是Spring,不太舒服
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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