题解 | #二叉排序树#

二叉排序树

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

#include <iostream>
using namespace std;

struct node{
    int val;
    node* right,*left;
    node(int x):val(x),left(nullptr),right(nullptr){};
};

void insertnode(node *&a, int x)
{
    if(a == nullptr)
    {
        a = new node(x);
        return ;
    }
    if(x < a->val)
    {
        insertnode(a->left,x);
    }
    else if(x > a->val)
    {
        insertnode(a->right,x);
    }
}

void pre(node* tree){
        if(tree==nullptr)return;
        cout<<tree->val<<" ";
        pre(tree->left);
        pre(tree->right);
}
void mid(node* tree){
    if(tree==nullptr)return;
        mid(tree->left);
        cout<<tree->val<<" ";
        mid(tree->right);
    }
void post(node* tree){
    if(tree==nullptr)return;
        post(tree->left);
        post(tree->right);
        cout<<tree->val<<" ";
    }

int main() {
    int a;
    while (cin >> a) { 
        node *tree = nullptr;
        int x;
        for(int i = 0;i < a; i++)
        {
            cin>>x;
            insertnode(tree,x);
        }
        pre(tree);
        cout<<endl;
        mid(tree);
        cout<<endl;
        post(tree);
        cout<<endl;
    }
}

全部评论

相关推荐

12-18 19:36
已编辑
门头沟学院 Java
程序员牛肉:可以的,简历没毛病了。 虽然还是偏向同质化,不过学历不错。后续我觉得重心放到刷实习+摆脱同质化问题上
实习简历求拷打
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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