#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 | 反转二叉树的更多相关文章

  1. leetCode之旅(12)-反转二叉树

    背景描述 Homebrew 是 OS X 平台上的包管理工具.用其官网的话说就是: the missing package manager for OS X | OS X 平台遗失的包管理器. 相信在 ...

  2. leetCode题解之反转二叉树

    1.题目描述 经典的反转二叉树,就是将二叉树中每个节点的左.右儿子交换. 2.题目分析 3.代码 TreeNode* invertTree(TreeNode* root) { if(root == N ...

  3. 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 ...

  4. 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 ...

  5. Java反转二叉树

    // 二叉树节点定义 public class BinaryTreefanzhuan { class TreeNode{ int value; TreeNode left; TreeNode righ ...

  6. 156. Binary Tree Upside Down反转二叉树

    [抄题]: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left nod ...

  7. 第27题:Leetcode226: Invert Binary Tree反转二叉树

    翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1  思路 如果根节点存在,就交换两个子树的根节点,用递归 ...

  8. LeetCode刷题笔记-递归-反转二叉树

    题目描述: 翻转一棵二叉树. 解题思路: 1.对于二叉树,立马递归 2.先处理 根节点,不需改动 3.处根的左子树和右子树需要交换位置 4.递归处理左子树和右子树.步骤见1-3步 Java代码实现: ...

  9. LeetCode Invert Binary Tree 反转二叉树

    思路:递归解决,在返回root前保证该点的两个孩子已经互换了.注意可能给一个Null. C++ /** * Definition for a binary tree node. * struct Tr ...

随机推荐

  1. go ---switch语句

    package main import ( "fmt" ) func main() { var ar = [...]string{"A", "B&qu ...

  2. - Git常用命令 基础 总结 MD

    目录 目录 Git常用命令 帮助 help 常用操作 初始化 clone init 提交 push 暂存 更新 撤销修改 分支 branch 查看分支 创建分支 切换分支 checkout 删除分支 ...

  3. Docker 运行 MySQL,使用 docker-compose

    目录结构 . │ .env │ docker-compose.yml │ └─mysql ├─config │ my.cnf │ └─data mysql 目录下的 data 为数据目录,mysql ...

  4. centOS 在线安装lnmp

    CentOS7源码安装最新版LNMP环境   lnmp环境版本如下: 系统:CentOS 7 x86_64 NGINX:nginx-1.7.12 数据库:mariadb-10.0.13 PHP:php ...

  5. RabbitMQ学习之RPC(6)

    在第二个教程中,我们了解到如何在多个worker中使用Work Queues分发费时的任务. 但是,如果我们需要在远程运行一个函数并且等待结果该怎么办呢?这个时候,我们需要另外一个模式了.这种模式通常 ...

  6. Python 序列、列表(List)、元组(Tuple)

    序列 序列是Python中最基本的数据结构,包括字符串.列表.元组. 序列,顾名思义,是有序的,序列都有索引,都能进行索引.切片(截取).加(连接).乘(倍增).检查成员的操作. 因为序列有序,可通过 ...

  7. Golang: 接收GET和POST参数

    GET 和 POST 是我们最常用的两种请求方式,今天结合前端 axios 请求库来讲一讲,如何在 golang 服务中,正确接收这两种请求的参数信息. 一.搭建一个简单的服务 首先,我们来创建一个最 ...

  8. React: 有状态组件生成真实DOM结点

    上次我们分析了无状态组件生成 DOM 的过程,无状态组件其实就是纯函数,它不维护内部的状态,只是根据外部输入,输出一份视图数据.而今天我们介绍的有状态组件,它有内部的状态,因此在组件的内部,可以自行对 ...

  9. JDBC连接时出现的两个错误

    这两个错误都是因为版本的更新导致的: 错误代码: package FirstTest; import java.sql.*; public class FirstJDBC { public stati ...

  10. java 时间格式

    自定义时间格式:yyyy 年MM 月dd 天HH 24小时制hh 12小时制mm 分钟ss 秒 java.util.Date日期格式为:年月日时分秒 java.sql.Date日期格式为:年月日jav ...