首页 > 代码库 > Q114第一颗二叉查找树(链式)

Q114第一颗二叉查找树(链式)

输入n,然后n个树,建立二叉查找树。从小到大输出每个节点的左右子树,空输出#

技术分享

#include<cstdio>
#include<iostream>
using namespace std;
typedef struct node{
    int data;
    struct node *lchild,*rchild;
}NODE;
void input(NODE *root,int value){
    if(value=http://www.mamicode.com/=root->data){"%d(",root->data);
        if(root->lchild==NULL){
            printf("#");
        }
        else{
            printf("%d",root->lchild->data);
        }
        if(root->rchild==NULL){
            printf(", #)\n");
        }
        else{
            printf(", %d)\n",root->rchild->data);
        }
        count++;
    }
    preorder(root->rchild);
}
int main(){
    NODE *root=new NODE;
    root->lchild=root->rchild=NULL;
    int a;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&a);
        input(root,a);
    }
    preorder(root);
    return 0;
}

  

Q114第一颗二叉查找树(链式)