首页 > 试题广场 >

二叉排序树

[编程题]二叉排序树
  • 热度指数:20398 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
二叉排序树,也称为二叉查找树。可以是一颗空树,也可以是一颗具有如下特性的非空二叉树: 1. 若左子树非空,则左子树上所有节点关键字值均不大于根节点的关键字值; 2. 若右子树非空,则右子树上所有节点关键字值均不小于根节点的关键字值; 3. 左、右子树本身也是一颗二叉排序树。 现在给你N个关键字值各不相同的节点,要求你按顺序插入一个初始为空树的二叉排序树中,每次插入后成功后,求相应的父亲节点的关键字值,如果没有父亲节点,则输出-1。

输入描述:
输入包含多组测试数据,每组测试数据两行。
第一行,一个数字N(N<=100),表示待插入的节点数。
第二行,N个互不相同的正整数,表示要顺序插入节点的关键字值,这些值不超过10^8。


输出描述:
输出共N行,每次插入节点后,该节点对应的父亲节点的关键字值。
示例1

输入

5
2 5 1 3 4

输出

-1
2
2
5
3
#include <iostream>
using namespace std;

struct node{
	int num;
	node *left=NULL;
	node *right=NULL;
};

node* insert(node *t,int num,int store){
	if(t==NULL){
		cout<<store<<endl;
		t=new node();
		t->num=num;
		return t;
	}
	if(num<t->num)
		t->left=insert(t->left,num,t->num);
	else
		t->right=insert(t->right,num,t->num);
	return t;
}

int main(){
	int n;
	scanf("%d",&n);
	int st[n];
	for(int i=0;i<n;i++)
		scanf("%d",&st[i]);

	node *t=new node();
	t->num=st[0];
	cout<<-1<<endl;
	for(int i=1;i<n;i++)
		t=insert(t,st[i],-1);

	return 0;
}

编辑于 2020-01-21 23:06:54 回复(0)
#include<iostream>
using namespace std;

typedef struct Btree
{
    int v;
    Btree * lchild,*rchild;
    Btree(int v):v(v){}
};

Btree * insert(Btree * t,int v,int farther)//v为当前插入的节点
{
    if(t == NULL)//插入都在叶节点
    {
        t = new Btree(v);
        cout << farther << endl;
    }
    else if(v < t->v)
        t->lchild = insert(t->lchild, v, t->v);
    else
        t->rchild = insert(t->rchild, v, t->v);
    return t;
}

int main(void)
{
    int n;
    
    while(cin >> n)
    {
        Btree * t;
        for(int i = 0;i < n;i++)
        {
            int v;
            cin >> v;
            t = insert(t, v, -1);//从根开始,没有父亲节点输出-1
        }
    }
    return 0;
}

发表于 2021-03-07 19:28:14 回复(0)
#include <iostream>


using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
/**
 * @brief 
 * 
 * @param T 
 * @param key 
 * @return TreeNode 
 */
TreeNode *InsertBST(TreeNode *T,TreeNode *TFather, int val)
{
    if (T == NULL)
    {
        cout << TFather->val << endl;
        T = new TreeNode(val);
        // T->val = val;
        // T->left = T->right = NULL;
        return T;
    }
    else if (val < T->val)
    { 
        T->left = InsertBST(T->left, T,val);
    }
    else if (val > T->val)
    {
        T->right = InsertBST(T->right,T, val);
    }
    return T;
}
int main()
{
    int n;
    while (cin >> n)
    {
        int val;
        bool first = true;
        TreeNode root(val);
        cout << -1 << endl;
        while (n--)
        {
            cin >> val;
            if (first)
            {
                root.val = val;
                first = false;
            }
            InsertBST(&root, NULL,val);
        }
    }
    return 0;
}

发表于 2021-03-02 22:40:49 回复(0)
/*
*仍然是模拟排序树创建过程,插入函数每次保留一下父节点即可。
*/


#include<bits/stdc++.h>

using namespace std;

struct Node
{
    int v;
    struct Node* lchild,* rchild;
};
int n;

Node* newNode(int x)
{
    Node* t = new Node;
    t->v = x;
    t->lchild = t->rchild = nullptr;
    return t;
}

void Insert(Node*& root, int x, int f)
{
    if(root == nullptr)
    {
        cout << f << '\n'; root = newNode(x); return ;
    }
    if(root->v == x) return ;
    else if(root->v < x) Insert(root->rchild, x, root->v);
    else Insert(root->lchild, x, root->v);
}

int main()
{
    ios::sync_with_stdio(false);
    while(cin >> n)
    {
        int x; Node* root = nullptr;
        while(n--)
        {
            cin >> x; Insert(root, x, -1);
        }
    }
    return 0;
}

发表于 2021-01-22 11:27:09 回复(0)
方法一递归
方法二:循环,避免末尾递归
#include<iostream>
using namespace std;
struct TNode
{
	int data;
	TNode* left, * right;
	TNode(int d) :data(d), left(nullptr), right(nullptr) {}
};
int Insert(int node, TNode*& tree)//tree必须要用引用类型
{
	int father = -1;
	TNode* move = tree, * parent = nullptr;
	while (move != nullptr)//找要插入节点的父节点
	{
		parent = move;
		father = move->data;
		if (node > move->data)
			move = move->right;
		else
			move = move->left;
	}
	TNode* temp = new TNode(node);//临时节点
	if (parent == nullptr)//插入到二叉排序树中
		tree = temp;
	else if (node < parent->data)
		parent->left = temp;
	else
		parent->right = temp;
	return father;
}

int Insert2(int node, TNode*& tree, int& father)//递归
{
	if (tree == nullptr)
		tree = new TNode(node);
	else	
	{
		father = tree->data;
		if (node < tree->data)
			Insert2(node, tree->left,father);
		else
			Insert2(node, tree->right,father);
	}
	return father;
}

int main()
{
	int N, node;
	while (cin>>N)
	{
		TNode* tree = nullptr;
		int father = -1;
		for (int i = 0; i < N; i++)
		{
			cin >> node;
			//cout << Insert(node, tree) << endl;
			cout << Insert2(node, tree, father) << endl;
		}
	}
	return 0;
}

编辑于 2020-06-23 13:20:15 回复(0)
//照搬一下讨厌鬼大佬的题解, 增加一下印象
#include<stdio.h>
typedef struct TNode{
    int data;
    struct TNode *left;
    struct TNode *right;
}*BiTree,TNode;
int loc;
TNode T[101];
//建树
BiTree create()
{
    T[loc].left=T[loc].right=NULL;//初始化空树
    return &T[loc++];
}
//增加结点
BiTree Insert(BiTree T,int x,int father)
{
    if(!T){
        T=create();//建树
        T->data=x;
        printf("%d\n",father);
        return T;
    }
    if(x<T->data)
        T->left=Insert(T->left,x,T->data);
    if(x>T->data)
        T->right=Insert(T->right,x,T->data);
    return T;
}
int main()
{
    int n,tmp;
    while(scanf("%d",&n)!=EOF)
    {
        TNode *T=NULL;
        loc=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&tmp);
            T=Insert(T,tmp,-1);
        }
    }
    return 0;
}

发表于 2020-04-23 16:00:08 回复(0)
Java 
import java.util.Scanner;

public class Main {

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }
    
    static void creat(TreeNode root, int value){
        if (value<root.val){
            if (root.left==null) {
                root.left = new TreeNode(value);
                System.out.println(root.val);
            } else creat(root.left,value);
        }else {
            if (root.right==null){
                root.right= new TreeNode(value);
                System.out.println(root.val);
            }else creat(root.right,value);
        }
    }
    

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int n = scanner.nextInt();
            TreeNode root = new TreeNode(scanner.nextInt());
            System.out.println("-1");
            for (int i = 1; i < n; i++) {
                creat(root,scanner.nextInt());
            }
        }
    }
}


发表于 2020-03-18 18:10:27 回复(0)
#include <bits/stdc++.h>
using namespace std;

struct TreeNode{
	int data;
	TreeNode* lChild;
	TreeNode* rChild;
	TreeNode(int data):data(data),lChild(NULL),rChild(NULL){}
};

// 二叉排序树建树,将key插入二叉排序树中的正确位置
TreeNode* Insert(TreeNode*& root,int key,int father){
	if(root==NULL){ // 找到叶子节点,此时插入节点成功,那么直接输出其父节点的值
		root = new TreeNode(key);
		cout<<father<<endl;
	}else{
		if(key<root->data){
			root->lChild =  Insert(root->lChild,key,root->data);
		}else{
			root->rChild = Insert(root->rChild,key,root->data);
		}
	}
	return root;
}

int main(){
	int n;
	while(cin>>n){
		TreeNode* root = NULL; // 建立空树
		for(int i=0;i<n;i++){
			int key;
			cin>>key;
			root = Insert(root,key,-1);
		}
	}
	return 0;
}


发表于 2020-03-12 16:50:43 回复(0)
#include <stdio.h>
#include<stdlib.h>

using namespace std;

struct node{  //二叉树链式存储结构
    int data;
    node* lchild;
    node* rchild;
};

int Insert(node* &root,int x,node* parent){  //修改了的二叉查找树插入操作
    if(root==NULL){  //将x插入根节点为root的二叉树中,该根节点的父节点为parent,由于root可能为空,
        root=(node*)malloc(sizeof(node));  //为了能更改root的值,过root声明为引用
        root->data=x;
        root->lchild=root->rchild=NULL;
        if(parent==NULL)
            return -1;
        else
            return parent->data;
    }

    if(x<=root->data){
        return Insert(root->lchild,x,root);  //return一定要有,因为函数必须有返回值,否则编译不通过
    }
    else{
        return Insert(root->rchild,x,root);  //return一定要有,因为函数必须有返回值,否则编译不通过
    }
}

int main()
{
    int N=0,i=0,x;
    scanf("%d",&N);
    node* root=NULL,*parent=NULL;

    for(i=0;i<N;i++){
        scanf("%d",&x);
        printf("%d\n",Insert(root,x,parent));
    }

    return 0;
}

发表于 2019-03-10 23:27:12 回复(0)
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

struct ptree{
    long int v;
    ptree * lc;
    ptree * rc;
};
void inserttree(ptree * a,int b){
    if(b>a->v){         //如果插入数据比当前节点值大,插入右子树
        if(a->rc==NULL){    //如果右子树为空则插入
            ptree * node=(ptree *)malloc(sizeof(ptree));    //创建节点
            a->rc=node;    //右孩子指向该节点
            node->v=b;
            node->lc=NULL;node->rc=NULL;
        }
        else
            inserttree(a->rc,b);
    }
    else{
        if(a->lc==NULL){    //否则插入左子树
            ptree * node=(ptree *)malloc(sizeof(ptree));
            a->lc=node;        //左孩子指向该节点
            node->v=b;
            node->lc=NULL;node->rc=NULL;
        }
        else
            inserttree(a->lc,b);
    }
}
int outputf(ptree * a,int b){    //遍历时,输入头节点和要查找的值
    if(a->v!=b){        //如果头节点值不等于目标值
        ptree * node,* p=a;
        while(p->v!=b){    //遍历
            node=p;        //记录前一个节点
            if(b>p->v)
                p=p->rc;
            else
                p=p->lc;
        }
        return node->v;        //返回前一个节点值
    }
    else
        return -1;    //若头节点值等于目标值,返回 -1
}

int main()
{
    int N;
    cin>>N;
    long int a[N];
    for(int i=0;i<N;i++){
        cin>>a[i];
    }
    ptree * head;
    head=(ptree *)malloc(sizeof(ptree));    //构建第一个节点
    head->v=a[0];
    head->lc=NULL;head->rc=NULL;        //设置孩子指针为空
    for(int i=1;i<N;i++){         //插入数据
        inserttree(head,a[i]);
    }
    for(int i=0;i<N;i++){        //查找数据
        long int n=outputf(head,a[i]);
        cout<<n<<endl;
    }
    return 0;
}

编辑于 2019-02-24 21:14:23 回复(0)
import java.util.Scanner;
import java.util.ArrayList;
public class Main{
class binTree{

binTree left=null;
binTree right=null;
int val=-1;

binTree(int val){
this.val=val;
}
}

public void parentnode(ArrayList<Integer> a){
if (a.size()==0)
return;
binTree root=new binTree(a.get(0));
System.out.println("-1");
for (int i=1;i<a.size();i++)
sorttree(root,a.get(i));
}
public void sorttree(binTree root,int v){
if (root==null)
return;
if (v<=root.val){
if (root.left==null){
root.left=new binTree(v);
System.out.println(root.val);
}else {
sorttree(root.left,v);
}
}else {
if (root.right==null){
root.right=new binTree(v);
System.out.println(root.val);
}else
sorttree(root.right,v);
}
}
public static void main(String[] args){
Scanner scanr=new Scanner(System.in);
int n=scanr.nextInt();
scanr.nextLine();
String string=scanr.nextLine();
ArrayList<Integer> arrays= new ArrayList<Integer>();
for (int i=0;i<string.split(" ").length;i++){
arrays.add(Integer.parseInt(string.split(" ")[i]));
}
Main t=new Main();
t.parentnode(arrays);
}
}
//C版
#include<stdio.h>
#include<stdlib.h>

typedef struct binTree{
    binTree *left;
    binTree *right;
    int val;

}BinNode,*BTree;

BTree createBinTree(BTree root,int v){
    if(root==NULL)
        return root;
    if(v<root->val){
            if(root->left==NULL){
                BTree node=(BinNode *) malloc (sizeof(BinNode));
                node->left=NULL;
                node->right=NULL;
                node->val=v;
                root->left=node;
                printf("%d\n",root->val);
            }else{
                root->left=createBinTree(root->left,v);
            }
    }else{
            if(root->right==NULL){
                BTree node=(BinNode *)malloc(sizeof(BinNode));
                node->left=NULL;
                node->right=NULL;
                node->val=v;
                root->right=node;
                printf("%d\n",root->val);
            }else{
                root->right=createBinTree(root->right,v);
            }
    }
    return root;
}
int main(){
    int n;
    int i=0;
    int v;
    scanf("%d",&n);
    scanf("%d",&v);
    BTree root=(BinNode *)malloc(sizeof(BinNode));
    root->left=NULL;
    root->right=NULL;
    root->val=v;
    printf("-1\n");
    while(i<n-1){
        scanf("%d",&v);
        root=createBinTree(root,v);
        i++;
    }
    return 0;
}

编辑于 2018-06-01 13:05:59 回复(0)

注意:
(1)插入的时候要保存父节点的值
(2)开始新的一棵树的时候要将原来的二叉树的值清空

#include <iostream>
#include <stdlib.h>
using namespace std;

typedef struct BiNode{
    int v;
    struct BiNode *lchild, *rchild;
}BiNode, *BiTree;
int Insert(BiTree &T, int tmp, int pre)
{
    if(T==NULL){
        T = (BiTree)malloc(sizeof(BiNode));
        T->v = tmp;
        T->lchild = T->rchild = NULL;
        return pre;  //插入节点的时候返回父节点
    }
    else{
        if(tmp<T->v) return Insert(T->lchild, tmp, T->v);
        else return Insert(T->rchild, tmp, T->v);
    }
}
void Delete(BiTree &T)  //清空二叉树
{
    if(T!=NULL)
    {
        if(T->lchild==NULL && T->rchild == NULL)
            free(T);
        else{
            if(T->lchild!=NULL) Delete(T->lchild);
            if(T->rchild!=NULL) Delete(T->lchild);
        }
    }
    T = NULL;
}
int main()
{
    BiTree T = NULL;
    int n;
    while(cin>>n)
    {
        for(int i = 0; i<n; i++)
        {
            int tmp;
            cin>>tmp;
            cout<<Insert(T, tmp, -1)<<endl;   //插入操作多一个参数存放父节点
        }
        Delete(T);
    }

    return 0;
}
发表于 2018-03-23 16:39:30 回复(0)
#include<stdio.h>
#include<string.h>
struct Node{
Node *lchild;
Node *rchild;
int c;
}Tree[100];
int loc;
Node *creat(){
Tree[loc].lchild=Tree[loc].rchild=NULL;
return &Tree[loc++];
}
bool F;
Node *build(Node *T,int x){
if(T==NULL){
if(F) {
F=false;
printf("-1\n");
}
T=creat();
T->c=x;
return T;
}
if(x<T->c){
if(T->lchild==NULL) printf("%d\n",T->c);
T->lchild=build(T->lchild,x);
}
else{
if(T->rchild==NULL)
printf("%d\n",T->c);
T->rchild=build(T->rchild,x);
}
return T;
}

int main(){
int N;
while(scanf("%d",&N)!=EOF){
F=true;
loc=0;
Node *T=NULL;
for(int i=0;i<N;i++){
int t;
scanf("%d",&t);
T=build(T,t);
}
}
return 0;
}
编辑于 2018-03-05 23:01:30 回复(0)
#include<cstdio>
#include<iostream>
using namespace std;
struct Node{
	int val;
	Node*left=NULL;
	Node*right=NULL;
};
void buildTree(Node*&r,int a){
	if(r==NULL){
		r=new Node;
		r->left=r->right=NULL;
		r->val=a;
		cout<<"-1"<<endl;
	}
	else{
		if(a<r->val){
			if(r->left==NULL){
				Node*tmp=new Node; //左子树为空,就申请一个节点挂上去,
				tmp->left=tmp->right=NULL;
				tmp->val=a;
				r->left=tmp;
				cout<<r->val<<endl;
			}
			else{
				buildTree(r->left,a);
			}
		}
		else{
			if(r->right==NULL){
				Node*tmp=new Node;
				tmp->left=tmp->right=NULL;
				tmp->val=a;	
				r->right=tmp;
				cout<<r->val<<endl;
			}
			else
			buildTree(r->right,a);
		}	
       }
}

int main(){
	int n;
	int a;
	while(cin>>n){
		Node*root=NULL;
		while(n--){
			cin>>a;
	    	buildTree(root,a);
		}
	}
}

发表于 2017-08-20 13:48:30 回复(0)
#include <stdio.h>
#include <stdlib.h>
typedef struct BTNode{
	int data;
	struct BTNode *lchild;
	struct BTNode *rchild;
}BTNode;

/*	二叉排序树插入操作	*/
BTNode* BST_Insert(BTNode *T,int x){
	if(T==NULL){
		T = (BTNode*)malloc(sizeof(BTNode));
		T->data = x;
		T->lchild = T->rchild = NULL;
		return T;
	}else if(x < T->data){
		T->lchild = BST_Insert(T->lchild,x);
	}else {
		T->rchild = BST_Insert(T->rchild,x);
	}
	return T;
}


/* 先序遍历二叉树	*/
void PrintBT(BTNode *T){
	if(T!=NULL){
		printf("%d ",T->data);
		PrintBT(T->lchild);
		PrintBT(T->rchild);
	}
}

/*	打印其父节点的值	*/
void PrintFather(BTNode *T,int x){
	if(T!=NULL){
		if((T->lchild != NULL  &&  T->lchild->data == x)  ||  (T->rchild!=NULL &&  T->rchild->data == x)){
		//别忘了左右判断左右子树 均非空才行 
			printf("%d\n",T->data);
			return;
		}
		PrintFather(T->lchild,x);
		PrintFather(T->rchild,x);
	}
}

int main(){
	int n,x;
	while(scanf("%d",&n) != EOF){
        BTNode *T = NULL;
		for(int i=0;i<n;i++){
			scanf("%d",&x);
			T = BST_Insert(T,x);
			if(i==0){
				printf("-1\n");
			}else{
				PrintFather(T,x);
			}
		}
//		PrintBT(T);  //前序打印二叉树 
	}
	return 0;
}
//DEV 正常,然而却说我只有1%的通过

编辑于 2017-03-01 22:19:46 回复(3)
#include<stdio.h>

struct Node{
Node *lchild,*rchild;
int c;
}T[100];

int loc=0;
Node *creat(){
T[loc].lchild=T[loc].rchild=NULL;
return &T[loc++];
}

Node *Insert(Node *T,int x){
if(T==NULL){
T=creat();
T->c=x;
return T;
}
else if(x<T->c){
T->lchild=Insert(T->lchild,x);
}
else if(x>T->c){
T->rchild=Insert(T->rchild,x);
}
return T;
}


void PrintFather(Node *T,int x){
if(T!=NULL){
if((T->lchild!=NULL&&T->lchild->c==x)||(T->rchild!=NULL&&T->rchild->c==x)){
printf("%d\n",T->c);
return;
}
PrintFather(T->lchild,x);
PrintFather(T->rchild,x);
}
int main(){
int n;
while(scanf("%d",&n)!=EOF){
loc=0;
Node *T=NULL;
for(int i=0;i<n;i++){
int x;
scanf("%d",&x);
T=Insert(T,x);
if(i==0)printf("-1\n");
else PrintFather(T,x);
}
}
return 0;
}
发表于 2017-03-18 11:52:54 回复(0)

#include<stdio.h>  #include<stdlib.h>

typedef struct Bnode {
    int data;
    struct Bnode* lchild;
    struct Bnode* rchild;
}Bnode;

Bnode* insert(Bnode *T, int d, int &f)//在插入的时候顺便记录父节点的数据
{
    if (T == NULL)
    {
        T = (Bnode*)malloc(sizeof(Bnode));
        T->data = d;
        T->lchild = NULL;
        T->rchild = NULL;
    }
    else if (T->data>d)
    {
        f = T->data;
        T->lchild = insert(T->lchild, d, f);
    }
    else
    {
        f = T->data;
        T->rchild = insert(T->rchild, d, f);
    }
    return T;
}

int main()
{
    int n, f, d;
    while (scanf("%d", &n) != EOF)
    {
        Bnode *T = NULL;
        for (int i = 0; i<n; i++)
        {
            f = -1;
            scanf("%d", &d);
            T = insert(T, d, f);
            printf("%d\n", f);
        }
    }
    return 0;
} 

编辑于 2018-01-03 17:37:25 回复(1)
二叉排序树插入操作稍作修改。
#include<stdio.h>
typedef struct TNode{
    int data;
    struct TNode *left,*right;
}*BiTree,TNode;
int loc;
TNode T[101];
BiTree creat(){
    T[loc].left=T[loc].right=NULL;
    return &T[loc++];
}
BiTree Insert(BiTree T,int x,int father){
    if(!T){
        T=creat();
        T->data=x;
        printf("%d\n",father);
        return T;
    }
    if(x<T->data)
        T->left=Insert(T->left,x,T->data);
    else if(x>T->data)
        T->right=Insert(T->right,x,T->data);
    return T;
}
int main(){
    int n,tmp;
    while(scanf("%d",&n)!=EOF){
        TNode *T=NULL;
        loc=0;
        for(int i=0;i<n;i++){
            scanf("%d",&tmp);
            T=Insert(T,tmp,-1);
        }
    }
    return 0;
} 

发表于 2018-01-10 21:01:56 回复(1)

取巧的办法,效率不高

思路:

若插入的结点比父节点大,则插入2i+1的位置,
若比父节点小,则插入到2i的位置(数组要开很大,不然过不了)

代码:

#include<iostream>

using namespace std;

void binTree(int *a,int num)
{
    int i = 1;
    while(a[i])
    {
        if(num>a[i])
            i = 2*i+1;
        if(num<a[i])
            i = 2*i;
    }
    a[i] = num;
    if(i!=1)
    {
        cout<<a[i/2]<<endl;
    }
    else
        cout<<"-1"<<endl;
}

int main()
{
    int a[1000000] = {0};
    int n;
    int temp;
    cin>>n;
    for(int i = 0;i<n;i++)
    {
        cin>>temp;
        binTree(a,temp);
    }

    return 0;
}
发表于 2018-03-03 21:05:16 回复(1)
想了许久没有思路,看了书上的相关解答,原来在递归建树的过程中返回父节点比较容易理解😁
//Binary Search Tree
#include<iostream>
#include<cstdio>

using namespace std;

struct TreeNode{
    int data;
    TreeNode* leftChild;
    TreeNode* rightChild;
    TreeNode(int x): data(x),leftChild(NULL),rightChild(NULL){}
};

TreeNode* Insert(TreeNode* root,int x,int father){
    if(root==NULL){
        root = new TreeNode(x);
        cout<<father<<endl;
    }
    else if(x<root->data){
        root->leftChild = Insert(root->leftChild,x,root->data);
    }
    else if(x>root->data){
        root->rightChild = Insert(root->rightChild,x,root->data);
    }
    return root;
}

int main(){
    int N;
    int temp;
    TreeNode* root = NULL;
    cin>>N;
    for(int i=0; i<N; i++){
        cin>>temp;
        root = Insert(root,temp,-1);
    }
    return 0;
}


发表于 2020-02-16 16:25:18 回复(0)