九度oj 1521 二叉树的镜像
原题链接:http://ac.jobdu.com/problem.php?pid=1521
水题,如下。。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
using std::cin;
using std::swap;
using std::vector;
const int Max_N = ;
struct Node {
int v, rev;
Node *fa, *ch[];
inline void set(int _v, Node *p) {
v = _v, rev = ;
fa = ch[] = ch[] = p;
}
inline void link(Node *x, bool d) {
ch[d] = x;
x->fa = this;
}
inline void update() {
rev ^= ;
swap(ch[], ch[]);
}
inline void push_down() {
if (rev != ) {
ch[]->update();
ch[]->update();
rev ^= ;
}
}
};
struct BinaryTree {
Node *root, *null, *tail, *ptr[Max_N], stack[Max_N];
void init() {
tail = &stack[];
null = tail++;
null->set(, NULL);
root = null;
}
inline Node *newNode(int v) {
Node *p = tail++;
p->set(v, null);
return p;
}
inline void gogo(int n) {
char c;
int i, v, a, b;
for (i = ; i <= n; i++) {
scanf("%d", &v);
ptr[i] = newNode(v);
}
for (i = ; i <= n; i++) {
cin >> c;
if (c == 'd'){
scanf("%d %d", &a, &b);
ptr[i]->link(ptr[a], );
ptr[i]->link(ptr[b], );
} else if (c == 'l') {
scanf("%d", &a);
ptr[i]->link(ptr[a], );
} else if (c == 'r') {
scanf("%d", &b);
ptr[i]->link(ptr[b], );
}
if (ptr[i]->fa == null) root = ptr[i];
}
}
inline void PreOder(Node *x, vector<int> &res) {
if (x != null) {
x->push_down();
res.push_back(x->v);
PreOder(x->ch[], res);
PreOder(x->ch[], res);
}
}
inline void PreOder() {
vector<int> res;
if (root == null) {
puts("NULL");
return;
}
root->update();
PreOder(root, res);
int n = res.size();
for (int i = ; i < n; i++) {
printf("%d%c", res[i], i < n - ? ' ' : '\n');
}
}
}tree;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n;
while (~scanf("%d", &n)) {
tree.init();
tree.gogo(n);
tree.PreOder();
}
return ;
}
九度oj 1521 二叉树的镜像的更多相关文章
- 九度oj 1184 二叉树遍历
原题链接:http://ac.jobdu.com/problem.php?pid=1184 简单的二叉树重建,遍历. 如下: #include<cstdio> #include<cs ...
- 九度OJ 1541 二叉树【数据结构】
题目地址:http://ac.jobdu.com/problem.php?pid=1541 题目描述: 旋转是二叉树的基本操作,我们可以对任意一个存在父亲节点的子节点进行旋转,包括如下几种形式(设被旋 ...
- [九度OJ]1078.二叉树的遍历(重建)
原题链接:http://ac.jobdu.com/problem.php?pid=1078 题目描述: 二叉树的前序.中序.后序遍历的定义:前序遍历:对任一子树,先访问跟,然后遍历其左子树,最后遍历其 ...
- [九度OJ]1113.二叉树(求完全二叉树任意结点所在子树的结点数)
原题链接:http://ac.jobdu.com/problem.php?pid=1113 题目描述: 如上所示,由正整数1,2,3……组成了一颗特殊二叉树.我们已知这个二叉树的最后一个结点是n.现在 ...
- 九度OJ 1113 二叉树
题目地址:http://ac.jobdu.com/problem.php?pid=1113 题目描述: 如上所示,由正整数1,2,3……组成了一颗特殊二叉树.我们已知这个二叉树的最后一个结点是n.现在 ...
- 九度OJ 1078 二叉树遍历
题目地址:http://ac.jobdu.com/problem.php?pid=1078 题目描述: 二叉树的前序.中序.后序遍历的定义: 前序遍历:对任一子树,先访问跟,然后遍历其左子树,最后遍历 ...
- 九度oj 1541 二叉树
原题链接:http://ac.jobdu.com/problem.php?pid=1541 简答题如下: #include<algorithm> #include<iostream& ...
- 剑指Offer - 九度1521 - 二叉树的镜像
剑指Offer - 九度1521 - 二叉树的镜像2013-11-30 23:32 题目描述: 输入一个二叉树,输出其镜像. 输入: 输入可能包含多个测试样例,输入以EOF结束.对于每个测试案例,输入 ...
- 【九度OJ】题目1113:二叉树 解题报告
[九度OJ]题目1113:二叉树 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1113 题目描述: 如上所示,由正整数1,2,3-- ...
随机推荐
- 【LeetCode】6. ZigZag Conversion 锯齿形转换
题目: 思路: 以图为例:s={'A','B','C','D','E','F','G','H'.....} 1.先不考虑中间元素F.G.H.N...,每一行前后元素在数组中对应下标相差size=2*n ...
- C++ 什么是句柄?为什么会有句柄?HANDLE
出处:http://www.cppblog.com/mymsdn/archive/2009/02/19/handle-in-windows.html 从广义上,能够从一个数值拎起一大堆数据的东西都可以 ...
- Exceeded maximum number of retries. Exceeded max scheduling attempts 3 for instance
Exceeded maximum number of retries. Exceeded max scheduling attempts 3 for instance
- WP8_ListBox的用法
在Windows Phone 7 Tips (5) 中曾经提到,在Windows Phone 7 中页面的布局一般分为:Panoramic.Pivot.List和Full Screen.而通常List ...
- css遇到的问题
1.屏幕三部分自适应居中?(js没奏效) 2.背景透明内容也透明?
- [python 2.7.5] 实现配置文件的读写
import ConfigParser config = ConfigParser.RawConfigParser() # When adding sections or items, add the ...
- windows7安装远程服务器AD域管理工具
目的:在win7上安装“远程服务器管理工具”,这样可以在客户端进行对服务器的AD域的操作,避免了远程登陆进服务器的麻烦. 前提条件:一般此工具只有管理员才具有有效使用权限,所以,在域administr ...
- AJAX验证用户是否存在
<html> <head> <title> ajax验证 </title> </head> <body> <input t ...
- 基于s5pv210嵌入式系统busybox文件系统移植
基于s5pv210嵌入式系统busybox文件系统移植 1.下载源码 busybox.net/downloads下载最新版的busybox源码,最新源码为1.21.1 2.解压源码文件 tar xvf ...
- banner淡出效果
<div class="banner"> <div class="ban"></div> <ul class=&quo ...