题目1521:二叉树的镜像
2014-11-25 16:28
197 查看
#include <cstdio> #include <cstdlib> #include <iostream> using namespace std; int n,L,R; int first=1; int Tree_val[1010]; struct Node { int val; Node * left,* right; }* Tree[1010]; void Build(Node * head,char ci,int val) { head->val=val; if(ci=='d') { Tree[L]=head->left =(Node *)malloc(sizeof(Node)); Tree[R]=head->right=(Node *)malloc(sizeof(Node)); } if(ci=='l') { Tree[L]=head->left =(Node *)malloc(sizeof(Node)); head->right=NULL; } if(ci=='r') { head->left =NULL; Tree[R]=head->right=(Node *)malloc(sizeof(Node)); } if(ci=='z') { head->left =NULL; head->right=NULL; } } void Print(Node * head) { if(first) { printf("%d",head->val); first=0; } else { printf(" %d",head->val); } if(head->right) { Print(head->right); } if(head->left) { Print(head->left); } } int main() { int i,n; char ci; while(~scanf("%d",&n)) { for(i=1;i<=n;i++) { cin>>Tree_val[i]; } Tree[1]=(Node *)malloc(sizeof(Node)); for(i=1;i<=n;i++) { cin>>ci; if(ci=='d') { // scanf("%d %d",&L,&R); cin>>L>>R; } if(ci=='l') { //scanf("%d",&L); cin>>L; } if(ci=='r') { //scanf("%d",&R); cin>>R; } Build(Tree[i],ci,Tree_val[i]); } if(n==0) { printf("NULL\n"); } else { Print(Tree[1]); printf("\n"); } } } /************************************************************** Problem: 1521 User: 萧然677 Language: C++ Result: Accepted Time:0 ms Memory:1532 kb ****************************************************************/这题很水,按root ,root->right , root->left顺序输出即可。。开始时scanf函数取字符时回车有影响,试了几个方法,没有成功,惭愧惭愧!!最后索性改成iostream了。。。。
相关文章推荐
- 题目1521:二叉树的镜像-九度
- 【九度】题目1521:二叉树的镜像
- 九度OJ-题目1521:二叉树的镜像
- 题目1521:二叉树的镜像
- 题目1521:二叉树的镜像
- 九度 题目1523:从上往下打印二叉树 题目1521:二叉树的镜像
- 题目1521:二叉树的镜像
- [Jobdu] 题目1521:二叉树的镜像
- 九度 题目1521:二叉树的镜像
- 剑指Offer题目1521:二叉树的镜像
- 【剑指Offer面试编程题】题目1521:二叉树的镜像--九度OJ
- 【剑指Offer面试编程题】题目1521:二叉树的镜像--九度OJ
- 题目1521:二叉树的镜像
- 算法题目-二叉树的镜像
- 九度 1521:二叉树的镜像
- 剑指OFFER之二叉树的镜像(九度OJ1521)
- 【九度OJ1521】|【剑指offer19】二叉树的镜像
- 1521二叉树的镜像
- 九度oj 1521 二叉树的镜像
- 剑指Offer 1521 二叉树的镜像