1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 数据结构二叉树学习1-前序序列创建二叉树

数据结构二叉树学习1-前序序列创建二叉树

时间:2023-02-11 12:59:23

相关推荐

数据结构二叉树学习1-前序序列创建二叉树

输入:

abc##de#g##f###

输出:

c b e g d f a

代码:

#include <iostream>#include <cstring>using namespace std;string input;struct node{char data;node *lchild;node *rchild;};void push(node * &root,int & index);void inorderpop(node * root);int main(){while(cin>>input){node * root=NULL;int index=0;push(root,index);inorderpop(root);cout<<endl;//必须是这个 不能return0}return 0;}void push(node * &root,int & index){if(index>=input.length()) return;//输入为空情况if(input[index]=='#'){root=NULL;index++;}else{root=new node;root->data=input[index];index++;push(root->lchild,index);push(root->rchild,index);}}void inorderpop(node * root){if(NULL==root) return;inorderpop(root->lchild);cout<<root->data<<" ";inorderpop(root->rchild);}

代码参考:

/xiongmao-cpp/p/6430108.html

cout表示向屏幕输出一个回车。 而return 0 or return 1 可用来标志函数的返回值,你可以用它来判断函数执行的情况.

cout<<endl;几乎等于printf("\n"); return 0和return

1如果是在main里面,意味着向操作系统返回一个调用该程序后的代码(可以用来表示错误信息),有时程序中不加它们,编译器会默默地为你加上它们的。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。