九度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-- ...
随机推荐
- 学习总结 java 父子级
package com.hanqi; //父类 public class Father { // public Father() // { // // } // public Father(Strin ...
- ionic pull to refresh 下拉更新頁面
有些項目都用到了下拉更新頁面的效果: 1. 在index.html 中添加ion-refresher 指令 且在我們需要更新內容的外面 添加 如 <ion-refresher pulling-t ...
- openstack实例热迁移
[DEFAULT]scheduler_default_filters=AllHostsFilterallow_resize_to_same_host=Trueallow_migrate_to_same ...
- leetcode 38
38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows:1, 11, ...
- 在WP8项目中使用ARMASM
由于之前项目中某些密集运算优化的需要,涉及到ARMASM相关的内容, 所以有幸可以在此分享一下自己的经验. 先铺垫一些知识: 1. ARM处理器有两种指令ARM.THUMB, 在WP8下默认是THUM ...
- php7对redis的扩展及redis主从搭建
这两天在学习一下php7下面的安装及redis相关配置认识.并将笔记记下来.以备后用.主要涉及到redis的扩展php-redis 及redis主从的配置. 一:redis安装 1:下载并安装 ...
- 在PHP项目中使用Standford Moss代码查重系统
Standford Moss 系统是斯坦福大学大名鼎鼎的代码查重系统,它可以查出哪些同学提交的代码是抄袭别人的,从而将提交结果拒之门外.它对一切希望使用该系统的人都是开放的,那么在PHP的项目中如何使 ...
- JQuery 的几个有用的技巧
JQuery代码 /* 新窗口打开链接:JQuery filter attr * 禁止鼠标弹出右键菜单:DOM contextmenu * 回到页面顶端:DOM scrollTo * 动态更换Css样 ...
- 安装CMS遇到php5.3的问题
DedeCMS Error: (PHP 5.3 and above) Please set 'request_order' ini value to include C,G and P (recomm ...
- C# 运行时编辑 节点重命名
方法一: ; bool nodeChanged = false; //右键点击,就进入修改状态 private void treeView1_NodeMouseClick(object sender, ...