【数据结构】【C++】二叉树的建立和先序遍历----(1)

今晚搞清楚了二叉树的建立和先序遍历,不过现在已经是0:32了,该睡觉了,贴上代码,明天补充细节,加油,晚安!

==============分割线=====================

#include<iostream>
#include <malloc.h>
#include<stdio.h>
using namespace std;
struct node
{
    int data;
    struct node* left;
    struct node* right;
};
void visit(node* BT);
node* creat();
node* creat(node* &T);
void preOrder(node*T);
int main()
{
    node* BT;
    BT=creat();
    preOrder(BT);
    return 0;
}
void visit(node* BT)
{
    cout<<BT->data<<' ';
    //cout<<"hi"<<endl;
}
node* creat()
{
    node* T;
    int num;
    scanf("%d",&num);
    if(num=='0')
        T=NULL;
    else
    {
        T = (node*)malloc(sizeof(node));
        T->data = num;
        //cout<<num;
        T->left = creat();
        T->right = creat();

    }
    return T;

}
void preOrder(node*T)
{
    if(T==NULL)
        return;
    else
    {
        visit(T);
        preOrder(T->left);
        preOrder(T->right);     
    } 
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-09 11:30
找工作7个月,投了7000封,3段世界五百强实习,才有一个offer,牛油们肯定比我强吧
码农索隆:不对不对不对,实习经历这么厉害,简历也没少投,问题出在哪呢
点赞 评论 收藏
分享
06-05 19:46
已编辑
武汉大学 后端
点赞 评论 收藏
分享
认真搞学习:这么良心的老板真少见
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-08 12:05
俺不中了,BOSS遇到了一个hr,我觉得我咨询的问题都很正常吧,然后直接就被拒绝了???
恶龙战士:你问的太多了,要不就整理成一段话直接问他,一个一个问不太好
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务