当前位置:网站首页>九度 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
边栏推荐
- 九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
- 一键部署Redis任意版本
- Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
- Version selection of boot and cloud
- 力扣 1037.有效的回旋镖
- gorilla官方:golang开websocket client的示例代码
- How to cooperate among multiple threads
- Flink并行度和Slot详解
- Force buckle 674 Longest continuous increasing sequence
- 力扣 1790. 仅执行一次字符串交换能否使两个字符串相等
猜你喜欢

Flink并行度和Slot详解

Open source heavy ware! Chapter 9 the open source project of ylarn causal learning of Yunji datacanvas company will be released soon!

Mrs offline data analysis: process OBS data through Flink job

Simulate the implementation of string class

Welcome to the markdown editor

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

mock.js从对象数组中任选数据返回一个数组

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

CSDN语法说明

YoloV6:YoloV6+Win10---训练自己得数据集
随机推荐
rk3128投影仪lcd显示四周显示不完整解决
Cloud 组件发展升级
搞定带WebKitFormBoundary post登录
About cv2 dnn. Readnetfromonnx (path) reports error during processing node with 3 inputs and 1 outputs [exclusive release]
Implement secondary index with Gaussian redis
Boot 和 Cloud 的版本选型
831. KMP字符串
Some arrangements about oneself
Equals method
How to implement safety practice in software development stage
CUDA versions are inconsistent, and errors are reported when compiling apex
第二十章 使用工作队列管理器(三)
Vulnhub's funfox2
MIT science and technology review article: AgI hype around Gato and other models may make people ignore the really important issues
c语言如何判定是32位系统还是64位系统
【mysql篇-基础篇】事务
力扣 643. 子数组最大平均数 I
Force buckle 912 Sort array
Graduation season | regretful and lucky graduation season
力扣599. 两个列表的最小索引总和