#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. 【WPF】1、 基本控件的简介

    WPF一直都是断断续续的使用.偶尔用到一下.但是每次间隔比较长,需要重新学习,就写了这篇日志.以后有问题,看这个就可以了解各大概,然后针对细节再另外想办法. 微软的东西真心好,如果什么都不懂,可以直接 ...

  2. 任意图像尺寸变成目标尺寸(包含相应的boxes的变换)

    def image_preporcess(image, target_size, gt_boxes=None): image = cv2.cvtColor(image, cv2.COLOR_BGR2R ...

  3. Java之路---Day12(多态)

    2019-10-26-22:40:09 目录: 1.多态的概念 2.多态的分类 3.实现多态的三个必要条件 4.多态的格式 5.多态成员变量的使用特点 6.多态成员方法的使用特点 7.多态的好处 8. ...

  4. VsCode中编写python环境配置

    1. VsCode中编写python环境配置 1.1. 前言 有过开发经验都知道idea一系列的软件虽然功能比较多,但比较容易卡,电脑不好还真容易上火,这里我想要入门python,还是选了款vscod ...

  5. Electron学习入门

    1.安装electron,不建议全局安装,这样每个app可以使用不同的electron版本了 2.配置package.json中的script下的start属性的值为electron . Electr ...

  6. 剑指前端(前端入门笔记系列)——Math对象

    Math对象 ECMAScript将一些常用的数学公式和信息封装到了一个对象中——Math对象,为我们实现数学方面的计算功能提供了便捷,而且该对象还提供了辅助完成这些计算的属性和方法   属性 con ...

  7. C/ C++ 快速上手

    C++ 快速上手 (一)https://www.cnblogs.com/cosmo89929/archive/2012/12/22/2828745.html C++ 快速上手 (二)https://w ...

  8. C#语法一些简化备忘

    有些传统的写法,可以简写,之前没留意到,现在才注意到 IDE0031: Null check can be simplified entity.Unit = entity.Unit == null ? ...

  9. Win10下免安装版MySQL8.0.16的安装和配置

    1.MySQL8.0.16解压 其中dada文件夹和my.ini配置文件是解压后手动加入的,如下图所示 2.新建配置文件my.ini放在D:\Free\mysql-8.0.16-winx64目录下 [ ...

  10. Kali 安装tightvncserver

    一.软件说明 a) tightvncserver是一个轻量级,只能建立桌面,不能查看TTY7/TTY1正在显示的桌面,但x11 vnc可以,相比x11vnc 安全传输差一些.反之,x11 vnc:安全 ...