不知道怎么回事下面的代码通过了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. visual leak dector内存泄漏检测方法

    http://vld.codeplex.com/ QT 内存泄露时,你们一般用什么工具检测啊 ------解决方案--------------------这篇你觉得详细么 :http://newfac ...

  2. Delphi通过IE窗口句柄获取网页接口(IWebBrowser2) good

    主要用到的是MSAA(Microsoft Active Accessibility) 函数:ObjectFromLResult,该函数在动态链接库 oleacc.dll 中定义. uses SHDoc ...

  3. 《Linux命令行与shell脚本编程大全》 第十四章 学习笔记

    第十四章:呈现数据 理解输入与输出 标准文件描述符 文件描述符 缩写 描述 0 STDIN 标准输入 1 STDOUT 标准输出 2 STDERR 标准错误 1.STDIN 代表标准输入.对于终端界面 ...

  4. nodejs在服务器上运行

     nodejs运行之后,关掉链接,网站运行就会断开,需要安装forever,后台执行. 安装方法如下(在windows和Linux下都能运行)://forever的安装: npm install fo ...

  5. GWT工程 —— HostedMode(宿主模式下调试) 所有的运行命令

    Unknown argument: -helpGoogle Web Toolkit 1.7.0HostedMode [-noserver] [-port port-number | "aut ...

  6. .Net平台-MVP模式初探(一)

    为什么要写这篇文章 笔者当前正在负责研究所中一个项目,这个项目基于.NET平台,初步拟采用C/S部署体系,所以选择了Windows Forms作为其UI.经过几此迭代,我们发现了一个问题:虽然业务逻辑 ...

  7. mysql alter example

    alter table `user_movement_log` AFTER `Regionid` (在哪个字段后面添加) ) default null; //主键 ) unsigned not nul ...

  8. 学习PS必须弄懂的专业术语

    在学习PS的过程中,我们经常会遇到一些专业术语,下面我们来对一些常用的.比较难理解的术语进行简单讲解. 像素:像素是构成图像的最基本元素,它实际上是一个个独立的小方格,每个像素都能记录它所在的位置和颜 ...

  9. Day 1: How to install jedi/codeintel plugin for sublime on Linux

    Step 1, Install sublime3 Download sublime2/3 from http://www.sublimetext.com/ $tar -jxvf sublime_tex ...

  10. String的构造函数、析构函数和赋值函数

    编写类String的构造函数.析构函数和赋值函数 已知类String的原型为: class String { public: String(const char *str = NULL); // 普通 ...