[Jobdu] 题目1521:二叉树的镜像
不知道怎么回事下面的代码通过了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:二叉树的镜像的更多相关文章
- 剑指Offer - 九度1521 - 二叉树的镜像
剑指Offer - 九度1521 - 二叉树的镜像2013-11-30 23:32 题目描述: 输入一个二叉树,输出其镜像. 输入: 输入可能包含多个测试样例,输入以EOF结束.对于每个测试案例,输入 ...
- 九度oj 1521 二叉树的镜像
原题链接:http://ac.jobdu.com/problem.php?pid=1521 水题,如下.. #include<algorithm> #include<iostream ...
- 剑指Offer面试题:18.二叉树的镜像
一.题目:二叉树的镜像 题目:请完成一个函数,输入一个二叉树,该函数输出它的镜像.例如下图所示,左图是原二叉树,而右图则是该二叉树的镜像. 该二叉树节点的定义如下,采用C#语言描述: public c ...
- 九度oj题目1521:二叉树的镜像
题目1521:二叉树的镜像 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:2061 解决:560 题目描述: 输入一个二叉树,输出其镜像. 输入: 输入可能包含多个测试样例,输入以EOF ...
- 剑指Offer 二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...
- (剑指Offer)面试题19:二叉树的镜像
题目: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树的定义如下: struct TreeNode{ int val; TreeNode* left; TreeNode* right; }; 输 ...
- 《剑指offer》— JavaScript(18)二叉树的镜像
二叉树的镜像 题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 相关知识 二叉树的镜像定义: 源二叉树 镜像二叉树 思路 有关二叉树的算法问题,一般都可以通过递归来解决.那么写一个正确的递归程序 ...
- 剑指offer——二叉树的镜像
题目:操作给定的二叉树,将其变换为源二叉树的镜像. 思路:前序(根左右的顺序)遍历一棵树,在存储的时候将其左右树进行交换,最后按照处理后的树还原,即得到其镜像. /** public class Tr ...
- 《剑指offer》 二叉树的镜像
本题来自<剑指offer>二叉树的镜像 题目: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 ...
随机推荐
- 解决:未能加载文件或程序集“Newtonsoft.Json, Version=4.5.0.0
开发环境:Microsoft Visual Studio 2013 MVC4.0 使用SignalR的过程中随着版本的升级,其它程序集也跟着升级,如Newtonsoft.Json,一般编译的时候,如果 ...
- 利用OpenCV和MFC对话框建设一个有滑动条控制的播放器--转
(一)问题的提出: OpenCV有一个很简单的播放视频文件并加载滑动条的程序,但是如何用MFC对话框来创建一个有滑动条控制的播放器呢,网络上四处搜索都没有代码可以参考,下的都是些骗子链接文件,很过分, ...
- poj2608---几个字母映射到同一个数字
#include <stdio.h> #include <stdlib.h> #include<string.h> ]={,,,,,,,,,,,,,,,,,,,,, ...
- 练习一下linux中的list函数。
所有的list函数见 include/linux/list.h 自己从 include/linux/list.h 拷贝了一些函数到自己的list.c中, 然后练习了一下. 没有别的目的,就是想熟练一下 ...
- JS isArray记录
var isArray=Function.isArray||function(0){ return typeof o === "object" && Object. ...
- WPF 采用Border创建圆角
通过设置可以创建圆角border的CornerRadius属性其边框呈现圆角样式 代码: <Border Height="50" Background="Red&q ...
- Oracle基础(二)---操作命令
接上篇博客介绍Oracle基本概要.以下将介绍数据库的操作指令. Sql*plus经常使用命令 连接命令 1. conn[ect] 使用方法 connusername/password@网路服务名[a ...
- C#将图片转化为黑白图片
最近项目需要将上传的图片转化为黑白图片 在网上找了很多资料,测试通过,上代码 using System; using System.Collections.Generic; using System. ...
- AlarmManager类的应用(实现闹钟功能)
1.AlarmManager,顾名思义,就是“提醒”,是Android中常用的一种系统级别的提示服务,可以实现从指定时间开始,以一个固定的间隔时间执行某项操作,所以常常与广播(Broadcast)连用 ...
- JavaScript知识(一)
首先想为大家分享两句话: 侧耳听智慧,专心求聪明,呼求明哲,扬声求聪明.——箴言2:2-3 你要保守你心,胜过保守一切,因为一生的果效,是由心发出.——箴言 4:23 ...O(∩_∩)O...今天学 ...