#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. 高亮显示小Demo

    public class ItemSearchImpl implements ItemSearchService { /** * 搜索 * * @param paramMap * @return */ ...

  2. 【开发笔记】-通过js控制input禁止输入空格

    <input type="text" id="fname" onkeyup="myFunction(id)"> <scri ...

  3. Delphi - ShellExecute资料

    Windows官方资料: https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea#p ...

  4. TensorFlow NMT的数据处理过程

    在tensorflow/nmt项目中,训练数据和推断数据的输入使用了新的Dataset API,应该是tensorflow 1.2之后引入的API,方便数据的操作.如果你还在使用老的Queue和Coo ...

  5. Python之路(第四十四篇)线程同步锁、死锁、递归锁、信号量

    在使用多线程的应用下,如何保证线程安全,以及线程之间的同步,或者访问共享变量等问题是十分棘手的问题,也是使用多线程下面临的问题,如果处理不好,会带来较严重的后果,使用python多线程中提供Lock ...

  6. VS code key shortcuts for windows

    mac上的快捷键,尽量是选择像我用vs studio上靠近. ctrl+K+S: 显示快捷键列 ctrl+shift+p: 系统配置命令行 ctrl+p:项目中文件列表,选择文件 Alt+M:当前文件 ...

  7. Emmagee的基本使用

    Emmagee的基本使用 注意:目前最新版本为2.5.1:由于谷歌限制仅支持安卓7.0以下版本: 一.Emmagee介绍 Emmagee是一个简单易上手的Android性能监测工具,主要用于监测单个A ...

  8. 环境搭建:Jupyter Notebook 密码设置

    原文参考:关于jupyter notebook密码设置 原文博主: 一.windows下,打开命令行,重新生成一个jupyter配置文件 jupyter notebook --generate-con ...

  9. Django 之restfromwork 序列化组件实现数据增删查改

    rest-framework序列化之Serializer models.py from django.db import models # Create your models here. class ...

  10. 关于子类和父类中的this的用法

    public class Demo { public static void main(String[] args) { Fu f = new Zi(); f.show(); } } class Fu ...