文章

13

粉丝

168

获赞

12

访问

12.2k

头像
二叉排序树2 题解:递归插入
P1411 华中科技大学机试题
发布于2023年6月15日 14:12
阅读数 637

递归插入

#include<bits/stdc++.h>
using namespace std;
#define rep(i,s,e) for(int i=s;i<e;i++)
#define per(i,s,e) for(int i=s;i>e;i--)

struct Tnode {
    int data;
    Tnode* lchild=NULL;
    Tnode* rchild=NULL;
};

Tnode* insert(Tnode* root,Tnode* t){
	if(root->data==t->data) return root;
	else if(root->data > t->data){
		if(root->lchild==NULL) {root->lchild=t;return root;}
		else return insert(root->lchild,t);
	}
	else{
		if(root->rchild==NULL) {root->rchild=t;return root;}
		else return insert(root->rchild,t);
	}
}

//1 6 5 9 8
Tnode* build_sorttree(Tnode* tree_root,vector<int> num){
    Tnode* root = NULL;
    rep(i,0,num.size()){
        Tnode* t=new Tnode; 
        t->data=num[i];
        if(i==0) root=t;
        else insert(root,t);
    }   
    return root;
}

void pre_travse(Tnode* t){
	if(t==NULL){
		return;
	}else{
		cout<<t->data<<' ';
		pre_travse(t->lchild);...
登录查看完整内容


登录后发布评论

1 条评论
snake
2024年3月13日 10:17

yes

赞(0)