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 ...
随机推荐
- -Shell 教程 Bash 脚本 基础语法 MD
目录 目录 Shell 简介 Shell 脚本 Shell 环境 第一个shell脚本 Shell 变量 定义变量 使用变量 只读变量 删除变量 Shell 字符串 单引号 双引号 字符串基本操作 S ...
- .Net Core 指定编码格式的问题
我们在读取txt文件时,如果文件格式不是utf8,则获取的中文会乱码,所以要么另存文件为utf8格式,要么使用和文件相同的编码来读取. 如果文件为utf8,则: //一种 StreamReader s ...
- Python进阶----索引原理,mysql常见的索引,索引的使用,索引的优化,不能命中索引的情况,explain执行计划,慢查询和慢日志, 多表联查优化
Python进阶----索引原理,mysql常见的索引,索引的使用,索引的优化,不能命中索引的情况,explain执行计划,慢查询和慢日志, 多表联查优化 一丶索引原理 什么是索引: 索引 ...
- 使用Python搭建http服务器
David Wheeler有一句名言:“计算机科学中的任何问题,都可以通过加上另一层间接的中间层解决.”为了提高Python网络服务的可移植性,Python社区在PEP 333中提出了Web服务器网关 ...
- JavaScript 运算符(Operator)
一.算数运算符 1.加法(+) 表示操作数相加: 处理特殊值规则: 如果两个操作数都是字符串,则将第二个操作数与第一个操作数拼接起来: 如果只有一个操作数是字符串,则将另一个操作数转换为字符串,然后 ...
- Android studio module生成jar包,module中引用的第三方库没有被引用,导致java.lang.NoClassDefFoundError错误。
android studio 创建了一个Module生成jar包,这个module中有引用一些第三方的类库,比如 gson,volley等. 但是生成的jar包里,并没有将gson,volley等第三 ...
- Axios的简单用法
一转眼Vue 3.0都要发布了,学习使用Vue也有一段时间了,记录一下axios的用法 Axios 是一个基于 Promise 的 HTTP 库,可以用在浏览器和 node.js 中,有点类似于aja ...
- golang reflect知识集锦
目录 反射之结构体tag Types vs Kinds reflect.Type vs reflect.Value 2019/4/20 补充 reflect.Value转原始类型 获取类型底层类型 遍 ...
- chrome开发者工具--使用 Network 面板测量您的网站网络性能。
转自:Tools for Web Developers Network 面板记录页面上每个网络操作的相关信息,包括详细的耗时数据.HTTP 请求与响应标头和 Cookie,等等. TL;DR 使用 ...
- admin端的专业管理模块功能测试
1.概述 1.1 测试范围 本次所测试的内容是admin端的专业管理模块. 1.2 测试方法 本次测试采用黑盒子方法进行集成测试. 1.3 测试环境 操作系统:Windows 2012 Server ...