题目1521:二叉树的镜像
来源:互联网 发布:人机界面数据记录 编辑:程序博客网 时间:2024/06/08 13:32
#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了。。。。
0 0
- 题目1521:二叉树的镜像
- 题目1521:二叉树的镜像
- 题目1521:二叉树的镜像
- 题目1521:二叉树的镜像
- 题目1521:二叉树的镜像
- 算法题目-二叉树的镜像
- 算法题目---二叉树的镜像
- 题目1521:二叉树的镜像-九度
- 【九度】题目1521:二叉树的镜像
- 九度 题目1521:二叉树的镜像
- 九度OJ-题目1521:二叉树的镜像
- 剑指Offer题目1521:二叉树的镜像
- 【剑指Offer面试编程题】题目1521:二叉树的镜像--九度OJ
- 1521二叉树的镜像
- 1521:二叉树的镜像 @jobdu
- 二叉树的镜像-jobdu-1521
- JD 1521:二叉树的镜像
- 二叉树的镜像
- win 8 和 Ubuntu 12.04 下goagent
- 《unix环境高级编程》信号——sigsetjmp 函数和 siglongjmp 函数
- RGB图像任意角度旋转
- javascript 函数详解2 -- arguments
- 进程-IPC 信号和信号量 (二)
- 题目1521:二叉树的镜像
- c++杂记
- 交叉编译器的信息查询
- 《unix高级环境编程》信号——sigsuspend 函数
- 第5章 列表
- FFMPEG:H264解码-SDL显示(RGB32、RGB24、YUV420P、YUV422)
- 配置IIS图解
- 理解Java的引用对象
- java 关于httpclient 请求https (如何绕过证书验证)