题解 | 二叉排序树

#include <iostream>
using namespace std;
typedef struct btree{
    int data;
    struct btree *left;
    struct btree *right;
}btree;
btree *insert(btree *t,int x)
{
  if(t==NULL)
  {
    t=new btree;
    t->data=x;
    t->left=t->right=NULL;
  }
  
  if(x<t->data)
  t->left=insert(t->left,x);
  if(x>t->data)
  t->right=insert(t->right,x);
  return t;
}
void preorder(btree *t)
{
  if(t==NULL)
  return;
  cout<<t->data<<" ";
  preorder(t->left);
  preorder(t->right);
}
void inorder(btree *t)
{
  if(t==NULL)
  return;
  inorder(t->left);
  cout<<t->data<<" ";
  inorder(t->right);
}
void postorder(btree *t)
{
  if(t==NULL)
  return;
  postorder(t->left);
  postorder(t->right);
  cout<<t->data<<" ";
}
int main() {
    int n;
    while(cin>>n)
    {btree *root=NULL;
    while(n--)
    {
        int x;cin>>x;
        root=insert(root,x);
    }
    preorder(root);
    cout<<endl;
    inorder(root);
    cout<<endl;
    postorder(root);
    cout<<endl;
    }
}

全部评论

相关推荐

07-02 10:44
门头沟学院 C++
码农索隆:太实诚了,告诉hr,你能实习至少6个月
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-03 18:22
投了几百份简历,专业和方向完全对口,都已读不回。尝试改了一下学校,果然有奇效。
steelhead:这不是很正常嘛,BOSS好的是即便是你学院本可能都会和聊几句,牛客上学院本机会很少了
点赞 评论 收藏
分享
Southyeung:我说一下我的看法(有冒犯实属抱歉):(1)简历不太美观,给我一种看都不想看的感觉,感觉字体还是排版问题;(2)numpy就一个基础包,机器学习算法是什么鬼?我感觉你把svm那些写上去都要好一点。(2)课程不要写,没人看,换成获奖经历;(3)项目太少了,至少2-3个,是在不行把网上学习的也写上去。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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