重生之我会用C加加了


私信TA

用户名:dotcpp0602879

访问量:3677

签 名:

放纵是本性,克制是智慧。

等  级
排  名 412
经  验 4808
参赛次数 3
文章发表 21
年  龄 21
在职情况 学生
学  校 江西科技师范大学
专  业 大数据

  自我简介:

我唯一会的而且擅长的也就只有Python,如果不能在Python领域取得好的成绩,那人生还有什么意义呢!

解题思路:

注意事项:

参考代码:


#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;


struct tree{

int v;

tree *left=NULL,*right=NULL;

};


bool cmp(tree t1,tree t2){

return t1.v<t2.v;

}


void pre(tree *dot){

cout<<dot->v<<" ";

if(dot->left) pre(dot->left);

if(dot->right) pre(dot->right);

}


void mid(tree *dot){

if(dot->left) mid(dot->left); 

cout<<dot->v<<" ";

if(dot->right) mid(dot->right);

}

void post(tree *dot){

if(dot->left) post(dot->left);

if(dot->right) post(dot->right);

cout<<dot->v<<" ";

}

void built(tree *root,tree *dot){

if(dot->v<root->v){

if(root->left) built(root->left,dot);

else root->left=dot;

}

else if(dot->v>root->v){

if(root->right) built(root->right,dot);

else root->right=dot; 

}

}

int main(){

int n;

while(cin>>n){

vector<tree> vt;

for(int i=0;i<n;i++){

int put;

cin>>put;

tree *te = new tree;

te->v=put;

vt.push_back(*te);

}


//sort(vt.begin(),vt.end(),cmp);

// 假设根节点 为vt[0]

for(int i=1;i<vt.size();i++){

built(&vt[0],&vt[i]);

}

// 输出结果

pre(&vt[0]);

cout<<"\n";

mid(&vt[0]);

cout<<"\n";

post(&vt[0]);

cout<<"\n";

}

}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区