A1102 | 反转二叉树
#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <map> #define I scanf
#define OL puts
#define O printf
#define F(a,b,c) for(a=b;a<c;a++)
#define FF(a,b) for(a=0;a<b;a++)
#define FG(a,b) for(a=b-1;a>=0;a--)
#define LEN 100
#define MAX 0x06FFFFFF
#define V vector<int> using namespace std; int used[LEN];
int cnt=; typedef struct Node{
struct Node *l=NULL;struct Node *r=NULL;
int d;
Node(){}
Node(int D){d=D;}
Node(Node *obj){
if(obj){
d=obj->d;
l=obj->l;
r=obj->r;
}
}
}Node; typedef struct inputInfo{
int index;string l;string r;
}inputInfo; inputInfo infos[LEN];
Node * nodes[LEN]; Node * createNodes(int index);
void inOrder(Node * node);
Node * root;
void invert(Node* node);
void levelOrder(Node*node); string inStr="";
string levelStr=""; int main(){
// freopen("d:/input/A1102.txt","r",stdin);
int n;
scanf("%d",&n);
int i;
FF(i,n){
char ch1[LEN];char ch2[LEN];
I("%s%s",ch1,ch2);
infos[i].index=i;infos[i].l=ch1;infos[i].r=ch2;
}
FF(i,n){
root=createNodes(i);
if(cnt>=n) break;
}
invert(root);
inOrder(root);
levelOrder(root);
puts(levelStr.substr(,levelStr.size()-).c_str());
puts(inStr.substr(,inStr.size()-).c_str()); return ;
} void levelOrder(Node*node){
queue<Node*> q;
q.push(node);
while(!q.empty()){
Node* t=q.front();
q.pop();
char buffer[LEN];
sprintf(buffer,"%d ",t->d);
levelStr+=buffer;
if(t->l) q.push(t->l);
if(t->r) q.push(t->r);
}
} Node * createNodes(int index){
if(used[index]) return NULL;
cnt++;
used[index]=;
int d=infos[index].index;
inputInfo f=infos[index];
Node *node =new Node(d);
if(f.l!="-"){
int i=;
sscanf(f.l.c_str(),"%d",&i); if(used[i]){
node->l=nodes[i];
}else{
node->l=createNodes(i);
}
}
if(f.r!="-"){
int i=;
sscanf(f.r.c_str(),"%d",&i); if(used[i]){
node->r=nodes[i];
}else{
node->r=createNodes(i);
}
}
nodes[index]=node;
return node;
} void inOrder(Node * node){
if(node){
inOrder(node->l);
char buffer[LEN];
sprintf(buffer,"%d ",node->d);
inStr+=buffer;
inOrder(node->r);
}
} void invert(Node* node){
if(!node) return;
if(node->l) invert(node->l);
if(node->r) invert(node->r);
Node * t=NULL;
if(node->l)t=new Node(node->l);
node->l=NULL;
if(node->r)node->l=new Node(node->r);
node->r=t;
}
超简单的一道题,居然写了一个小时,还写了一百多行……明天一定要研究一下大佬们是怎么写的。
看了蓝书之后才知道原来可以这么写。
1.静态二叉树数据结构,左右叶子结点直接用Node数组的下标记录,空结点就记录1(在结构体中直接初始化为1)
2.反转二叉树的操作实际上是后序遍历
3.查找根节点的方法:先把数据都记录到Node数组中,在录数据的时候记录出现的叶子节点的ID,让isLeaf[ID]=0,这样直接找到整棵树都不是叶子的节点,就是根节点。
#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <map> #define I scanf
#define OL puts
#define O printf
#define F(a,b,c) for(a=b;a<c;a++)
#define FF(a,b) for(a=0;a<b;a++)
#define FG(a,b) for(a=b-1;a>=0;a--)
#define LEN 200
#define MAX 0x06FFFFFF
#define V vector<int> using namespace std; typedef struct Node{
int l=-,r=-;
}Node;
Node nodes[LEN];
int isLeaf[LEN];
int n;
int cnt; int getRoot(){
int i;
FF(i,n){
if(isLeaf[i]==)
return i;
}
} void buildTree(int i,char l,char r){
if(l!='-'){
int i_l=l-;
nodes[i].l=i_l;
isLeaf[i_l]=;
}
if(r!='-'){
int i_r=r-;
nodes[i].r=i_r;
isLeaf[i_r]=;
}
} void print(int id){
O("%d",id);
if(++cnt<n) O(" ");
} void invert(int i){
if(i>=){
invert(nodes[i].l);
invert(nodes[i].r);
swap(nodes[i].l,nodes[i].r);
}
} void bfs(int root){
cnt=;
queue<int> q;
q.push(root);
while(!q.empty()){
int t=q.front();
q.pop();
print(t);
if(nodes[t].l>=) q.push(nodes[t].l);
if(nodes[t].r>=) q.push(nodes[t].r);
}
} void inOrder(int i){
if(i>=){
inOrder(nodes[i].l);
print(i);
inOrder(nodes[i].r);
}
} int main(){
freopen("d:/input/A1102.txt","r",stdin);
scanf("%d",&n);
int i=n;
FF(i,n){
char l,r;
scanf("%*c%c %c",&l,&r);
buildTree(i,l,r);
}
int root=getRoot();
invert(root);
bfs(root);OL("");
cnt=;inOrder(root);OL("");
return ;
}
A1102 | 反转二叉树的更多相关文章
- leetCode之旅(12)-反转二叉树
背景描述 Homebrew 是 OS X 平台上的包管理工具.用其官网的话说就是: the missing package manager for OS X | OS X 平台遗失的包管理器. 相信在 ...
- leetCode题解之反转二叉树
1.题目描述 经典的反转二叉树,就是将二叉树中每个节点的左.右儿子交换. 2.题目分析 3.代码 TreeNode* invertTree(TreeNode* root) { if(root == N ...
- LeetCode OJ:Invert Binary Tree(反转二叉树)
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- LeetCode 226. Invert Binary Tree (反转二叉树)
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- Java反转二叉树
// 二叉树节点定义 public class BinaryTreefanzhuan { class TreeNode{ int value; TreeNode left; TreeNode righ ...
- 156. Binary Tree Upside Down反转二叉树
[抄题]: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left nod ...
- 第27题:Leetcode226: Invert Binary Tree反转二叉树
翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 思路 如果根节点存在,就交换两个子树的根节点,用递归 ...
- LeetCode刷题笔记-递归-反转二叉树
题目描述: 翻转一棵二叉树. 解题思路: 1.对于二叉树,立马递归 2.先处理 根节点,不需改动 3.处根的左子树和右子树需要交换位置 4.递归处理左子树和右子树.步骤见1-3步 Java代码实现: ...
- LeetCode Invert Binary Tree 反转二叉树
思路:递归解决,在返回root前保证该点的两个孩子已经互换了.注意可能给一个Null. C++ /** * Definition for a binary tree node. * struct Tr ...
随机推荐
- HTTP协议随笔
代理 代理就是处在客户端和服务端之间的服务器.客户端例如浏览器发送GET请求时,代理服务器接收该请求,并转发该请求至服务所在的服务器.服务器回复的数据和资源在第一时间经过代理服务器,才能回传到浏览器, ...
- lightGBM gpu环境配置
推荐先看一手官方的Installation Guide.我用的是ubuntu 16.04,一些要求如下图: 主要是OpenCL以及libboost两个环境的要求. (1) OpenCL的安装.我这里之 ...
- springMVC中controller层方法中使用private和public问题
楼主一直习惯使用public,偶尔手误也可能使用private,但是发觉也没啥区别,都能调用service层,注入bean. 后来做一个新项目时,发觉自己以前的写的部分功能报错,当时有点懵逼,,找了半 ...
- Java自学-接口与继承 接口
设计Java的接口 在设计LOL的时候,进攻类英雄有两种,一种是进行物理系攻击,一种是进行魔法系攻击 这时候,就可以使用接口来实现这个效果. 接口就像是一种约定,我们约定某些英雄是物理系英雄,那么他们 ...
- 虚拟环境和pip相关的命令
# 虚拟环境 mkvirtualenv # 创建虚拟环境 rmvirtualenv # 删除虚拟环境 workon # 进入虚拟环境.查看所有虚拟环境 deactivate # 退出虚拟环境 eg: ...
- JavaWeb第三天--JavaScript
JavaScript 1. JavaScript概述 1.1 JavaScript是什么?有什么作用? HTML:就是用来写网页的.(人的身体) CSS:就是用来美化页面的.(人的衣服) JavaSc ...
- npm ERR! code ELIFECYCLE webpack-dev-server --inline --progress --config build/webpack.dev.conf.js`
“E:\Program Files\JetBrains\WebStorm 2018.1.4\bin\runnerw.exe” G:\node\nodejs\node.exe G:\node\nodej ...
- Java 之 比较器( Comparator接口与 Comparable 接口)
一.定制排序:java.util.Comparator 接口 强行对某个对象 collection 进行整体排序 的比较函数.可以将 Comparator 传递给 sort 方法(如 Collecti ...
- Ubuntu下的log日志查看器
1.lnav:Linux 下一个基于控制台的高级日志文件查看器 https://www.cnblogs.com/michealLang/p/9761886.html http://www.imooc. ...
- 转摘jemeter学习-连接数据库之jdbc请求
JMETER连接数据库 mysql下载地址:https://dev.mysql.com/downloads/connector/j/ mysql连接器根据语言选择/J,用的是Mac,选择下载.tar. ...