文章

12

粉丝

0

获赞

8

访问

325

头像
二叉排序树 - 华科 题解:
P1396 华中科技大学
发布于2026年2月18日 23:41
阅读数 6

#include <bits/stdc++.h>
using namespace std;

struct node{
    int val;
    node* left;
    node* right;
};

void Insert(node* &root,int t,int father){
    if (root==nullptr){
        root=new node{t,nullptr,nullptr};
        cout<<father<<endl;
    }
    else if(t>=root->val){
        Insert(root->right,t,root->val);
    }
    else if(t<root->val){
        Insert(root->left,t,root->val);
    }
}

int main(){
    int N;
    cin>>N;
    node* tree=nullptr;
    for(int i=0;i<N;i++){
        int t;
        cin>>t;
        Insert(tree,t,-1);
    }
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发