不知道怎么回事下面的代码通过了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[n];
for (int i = ; 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 = ; i < n; ++i) {
char str[];
scanf("%s", str);
if (str[] == 'd') {
int left, right;
scanf("%d %d", &left, &right);
nodes[i]->lchild = nodes[left - ];
nodes[i]->rchild = nodes[right - ];
} else if (str[] == 'l') {
int left;
scanf("%d", &left);
nodes[i]->lchild = nodes[left - ];
} else if (str[] == 'r') {
int right;
scanf("%d", &right);
nodes[i]->rchild = nodes[right - ];
}
} return nodes[];
} /*
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 == ? printf("%d", root->key) : printf(" %d", root->key);
printTreeMirror(root->lchild, count + );
printTreeMirror(root->rchild, count + );
}
} int main() {
int n;
while (scanf("%d", &n) != EOF) {
int a[n];
for (int i = ; i < n; i++)
scanf("%d", &a[i]); BTNode *root = createBinaryTree(a, n);
printTreeMirror(root, );
printf("\n");
} return ;
}
/**************************************************************
    Problem: 1521
    User: tonyhu
    Language: C++
    Result: Runtime Error
****************************************************************/

[Jobdu] 题目1521:二叉树的镜像的更多相关文章

  1. 剑指Offer - 九度1521 - 二叉树的镜像

    剑指Offer - 九度1521 - 二叉树的镜像2013-11-30 23:32 题目描述: 输入一个二叉树,输出其镜像. 输入: 输入可能包含多个测试样例,输入以EOF结束.对于每个测试案例,输入 ...

  2. 九度oj 1521 二叉树的镜像

    原题链接:http://ac.jobdu.com/problem.php?pid=1521 水题,如下.. #include<algorithm> #include<iostream ...

  3. 剑指Offer面试题:18.二叉树的镜像

    一.题目:二叉树的镜像 题目:请完成一个函数,输入一个二叉树,该函数输出它的镜像.例如下图所示,左图是原二叉树,而右图则是该二叉树的镜像. 该二叉树节点的定义如下,采用C#语言描述: public c ...

  4. 九度oj题目1521:二叉树的镜像

    题目1521:二叉树的镜像 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:2061 解决:560 题目描述: 输入一个二叉树,输出其镜像. 输入: 输入可能包含多个测试样例,输入以EOF ...

  5. 剑指Offer 二叉树的镜像

    题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...

  6. (剑指Offer)面试题19:二叉树的镜像

    题目: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树的定义如下: struct TreeNode{ int val; TreeNode* left; TreeNode* right; }; 输 ...

  7. 《剑指offer》— JavaScript(18)二叉树的镜像

    二叉树的镜像 题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 相关知识 二叉树的镜像定义: 源二叉树 镜像二叉树 思路 有关二叉树的算法问题,一般都可以通过递归来解决.那么写一个正确的递归程序 ...

  8. 剑指offer——二叉树的镜像

    题目:操作给定的二叉树,将其变换为源二叉树的镜像. 思路:前序(根左右的顺序)遍历一棵树,在存储的时候将其左右树进行交换,最后按照处理后的树还原,即得到其镜像. /** public class Tr ...

  9. 《剑指offer》 二叉树的镜像

    本题来自<剑指offer>二叉树的镜像 题目: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 ...

随机推荐

  1. windows设置临时环境变量path

    所有在命令行下对环境变量的修改只对当前窗口有效,不是永久性的修改. 1.查看当前所有可以的环境变量:输入set查看 2.查看某个环境变量:输入 set 变量名 例如 set path 3.修改环境变量 ...

  2. 适配器模式—STL中的适配器模式分析

    适配器模式通常用于将一个类的接口转换为客户需要的另外一个接口,通过使用Adapter模式能够使得原本接口不兼容而不能一起工作的类可以一起工作. 这里将通过分析c++的标准模板库(STL)中的适配器来学 ...

  3. Java 覆盖测试工具 :EclEmma

    http://www.eclemma.org/installation.html#manual EclEmma 2.2.1 Java Code Coverage for Eclipse Overvie ...

  4. android 强制设置横屏 判断是横屏还是竖屏

    判断activity 是横屏还是竖屏  方法 1: //根据设备配置信息 Configuration cf= this.getResources().getConfiguration(); //获取设 ...

  5. 【WorkTile赞助】jQuery编程挑战#009:生成两个div元素互相追逐的动画

    HTML页面: <!-- HTML代码片段中请勿添加<body>标签 //--> <div id="container"> <div id ...

  6. MS SQL:ID自增列从1开始重新排序

    数据库中把ID自增长重置成1: 一般做法:(太麻烦) 复制表数据->删除原表.新建一张表->粘贴: 新方法: 数据库中:新建查询->复制.粘贴一下代码->修改表名,执行即可(先 ...

  7. [原创] 小而美 | Mac上鲜为人知,但极大提升效率的小工具

    热爱收集实用又好用的软件,工具类软件不在多,发挥作用,提高效率最重要~推荐几个压箱底的藏货 一.Noizio -自然而然的白噪声,专注工作 Noizio是一款OS X 下的白噪音应用,可以让自己觉着是 ...

  8. Windows窗体Winform----show()与showDialog()的区别

    show()与showDialog()的区别 最常见的显示一个新的窗体的方法有两种,下面直接上代码!! A.WinForm中窗体显示 显示窗体可以有以下2种方法:   Form.ShowDialog( ...

  9. rownum(转载)

    对于 Oracle 的 rownum 问题,很多资料都说不支持>,>=,=,between...and,只能用以上符号(<.<=.!=),并非说用>,>=,=,be ...

  10. unexpected token: null near line 1, column 290

    org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: null near line 1, column 290 ...