#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
struct TreeNode{
int data;
TreeNode *leftchild;
TreeNode *rightchild;
};
TreeNode* Insert(TreeNode* root, int x){//二叉排序树建树
if(root == NULL){
root = new TreeNode();
root->data = x;
root->leftchild = NULL;
root->rightchild = NULL;
}else if(x < root->data){
root->leftchild = Insert(root->leftchild,x);
}else{
root->rightchild = Insert(root->rightchild,x);
}
return root;
}
void InOrder(TreeNode* root){//二叉排序树中序输出升序所以叫排序树
if(root != NULL){
InOrder(root->leftchild);
printf("%d\n",root->data);
InOrder(root->rightchild);
}
return;
}
int main(){
int n;
while(scanf("%d",&n) != EOF){
TreeNode* root = NULL;
while(n--){
int x;
scanf("%d",&x);
root = Insert(root,x);
}
InOrder(root);
}
return 0;
}
二叉排序树建树 中序遍历输出升序
最新推荐文章于 2023-05-20 16:03:27 发布