当前位置:网站首页>九度 1201 -二叉排序数遍历- 二叉排序树「建议收藏」
九度 1201 -二叉排序数遍历- 二叉排序树「建议收藏」
2022-07-07 18:11:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
这个是道正统的树构建和遍历题。一開始还想用数组构建取代一下水过去,可是发现不行,仅仅好老老实实的用指针了。二叉排序树和遍历方法假设不清楚定义的话。最好去看看数据结构书复习下。
#include<stdio.h>
struct node{
node *l;
node *r;
int val;
node(int a):val(a),l(NULL),r(NULL){};
};
node *root;
int n;
void qian(node *p){
printf("%d ",p->val);
if(p->l!=NULL)qian(p->l);
if(p->r!=NULL)qian(p->r);
}
void zhong(node *p){
if(p->l!=NULL)zhong(p->l);
printf("%d ",p->val);
if(p->r!=NULL)zhong(p->r);
}
void hou(node *p){
if(p->l!=NULL)hou(p->l);
if(p->r!=NULL)hou(p->r);
printf("%d ",p->val);
}
int main(){
int val;
node *p;
while(~scanf("%d",&n)){
root=NULL;
for(int i=0;i<n;i++){
scanf("%d",&val);
if(i==0){
root=new node(val);
continue;
}
p=root;
while(1){
if(val==p->val)break;
else if(val<p->val){
if(p->l==NULL){
p->l=new node(val);
break;
}
else{
p=p->l;continue;
}
}
else if(val>p->val){
if(p->r==NULL){
p->r=new node(val);
break;
}
else{
p=p->r;continue;
}
}
}
}
qian(root);
printf("\n");
zhong(root);
printf("\n");
hou(root);
printf("\n");
}
return 0;
}发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116527.html原文链接:https://javaforall.cn
边栏推荐
- Leetcode force buckle (Sword finger offer 36-39) 36 Binary search tree and bidirectional linked list 37 Serialize binary tree 38 Arrangement of strings 39 Numbers that appear more than half of the tim
- 有了ST7008, 蓝牙测试完全拿捏住了
- [MySQL - Basic] transactions
- CJSON内存泄漏的注意事项
- How to cooperate among multiple threads
- 841. String hash
- R language dplyr package mutate_ At function and min_ The rank function calculates the sorting sequence number value and ranking value of the specified data column in the dataframe, and assigns the ra
- Try the tuiroom of Tencent cloud (there is an appointment in the evening, which will be continued...)
- 机器学习笔记 - 使用Streamlit探索对象检测数据集
- YoloV6:YoloV6+Win10---训练自己得数据集
猜你喜欢

vulnhub之tre1

openEuler 资源利用率提升之道 01:概论

php 获取图片信息的方法

编译器优化那些事儿(4):归纳变量

BI的边界:BI不适合做什么?主数据、MarTech?该如何扩展?

One click deployment of any version of redis

LeetCode_ 7_ five

Machine learning notes - explore object detection datasets using streamlit

Ways to improve the utilization of openeuler resources 01: Introduction

How to test CIS chip?
随机推荐
基于深度学习的目标检测的更新迭代总结(持续更新ing)
Read PG in data warehouse in one article_ stat
With st7008, the Bluetooth test is completely grasped
MRS离线数据分析:通过Flink作业处理OBS数据
php 获取图片信息的方法
One click deployment of any version of redis
Some important knowledge of MySQL
Welcome to the markdown editor
Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
一键部署Redis任意版本
Traversal of Oracle stored procedures
JVM GC垃圾回收简述
pom. Brief introduction of XML configuration file label function
Version selection of boot and cloud
Oracle 存儲過程之遍曆
九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
一. 基础概念
mock. JS returns an array from the optional data in the object array
Kubernetes——kubectl命令行工具用法详解
【STL】vector