[Jobdu] 题目1521:二叉树的镜像
2015-08-04 17:46
260 查看
不知道怎么回事下面的代码通过了4个测试用例,还有1个测试用例始终是Runtime Error,各位帮我看一下是哪里出了问题
镜像输出两种方法,一种是递归进行调整,另外一种就是直接在先序遍历的基础上进行改造,下面代码中实现的是第二种
镜像输出两种方法,一种是递归进行调整,另外一种就是直接在先序遍历的基础上进行改造,下面代码中实现的是第二种
#include <cstdio> #include <cstdlib> typedef struct BTNode{ int key; struct BTNode *lchild; struct BTNode *rchild; }BTNode; BTNode *createBinaryTree(int a[], int n) { BTNode *nodes ; for (int i = 0; i < n; ++i) { nodes[i] = (BTNode *) malloc(sizeof(BTNode)); nodes[i]->key = a[i]; nodes[i]->lchild = NULL; nodes[i]->rchild = NULL; } for (int i = 0; i < n; ++i) { char str[10]; scanf("%s", str); if (str[0] == 'd') { int left, right; scanf("%d %d", &left, &right); nodes[i]->lchild = nodes[left - 1]; nodes[i]->rchild = nodes[right - 1]; } else if (str[0] == 'l') { int left; scanf("%d", &left); nodes[i]->lchild = nodes[left - 1]; } else if (str[0] == 'r') { int right; scanf("%d", &right); nodes[i]->rchild = nodes[right - 1]; } } return nodes[0]; } /* void getTreeMirror(BTNode *root) { if (!root) return; if (!root->lchild && !root->rchild) return; BTNode *temp = root->lchild; root->lchild = root->rchild; root->rchild = temp; getTreeMirror(root->lchild); getTreeMirror(root->rchild); }*/ void printTreeMirror(BTNode *root, int count) { if (root) { count == 0 ? printf("%d", root->key) : printf(" %d", root->key); printTreeMirror(root->lchild, count + 1); printTreeMirror(root->rchild, count + 1); } } int main() { int n; while (scanf("%d", &n) != EOF) { int a ; for (int i = 0; i < n; i++) scanf("%d", &a[i]); BTNode *root = createBinaryTree(a, n); printTreeMirror(root, 0); printf("\n"); } return 0; } /************************************************************** Problem: 1521 User: tonyhu Language: C++ Result: Runtime Error ****************************************************************/
相关文章推荐
- OC学习----内存管理
- 怎样判断一个股权众筹项目是否靠谱?
- xcode6以上空模板配置
- 怎样判断一个股权众筹项目是否靠谱?
- Spring mvc 用FreeMarker模板化
- 人件札记:产品质量
- Mysql 创建数据库或表名是使用特殊符号
- C#将图片和字节流互相转换并显示到页面上
- html5游戏开发笔记
- swirl 2: Workspace and Files
- 关于excel数据来源单据和母单据之间的关联
- Non-debuggable application installed on the target device
- 关闭Tomcat时,产生“线程未关闭,出现内存泄漏”错误和"java.lang.IllegalStateException"
- uva 1615Highway 贪心
- centos查看系统登录记录
- 控制随机抽中几率 [ C# | Random ]
- HDU 4707--Pet【DFS && 邻接表】
- new Thread的弊端及Java四种线程池的使用
- 1006 堡垒问题
- Android 自定义ViewGroup实现弧形菜单