Invert a Binary Tree

pat-1102

import java.util.Arrays;
import java.util.Queue;
import java.util.Scanner;
import java.util.concurrent.LinkedBlockingQueue; /**
* @Author WaleGarrett
* @Date 2020/9/5 20:04
*/
public class PAT_1102 {
static InvertTree[] tree;
static boolean[] flag;
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
tree=new InvertTree[n];
flag=new boolean[n];
Arrays.fill(flag,false);
scanner.nextLine();
for(int i=0;i<n;i++){
String a=scanner.next(),b=scanner.next();
int na,nb;
if(a.equals("-")){
na=-1; }else {
na=Integer.parseInt(a);
flag[na]=true;
}
if(b.equals("-")){
nb=-1;
}else {
nb=Integer.parseInt(b);
flag[nb]=true;
}
tree[i]=new InvertTree();
tree[i].left=na;
tree[i].right=nb;
}
int head=-1;
for(int i=0;i<n;i++){
if(!flag[i]){
head=i;
break;
}
}
System.out.println(levelOrder(head,"").trim());
System.out.println(inOrder(head,"").trim());
}
public static String levelOrder(int head,String result){
Queue<Integer> que=new LinkedBlockingQueue<>();
que.add(head);
while(!que.isEmpty()){
int now=que.poll();
result=result+now+" ";
if(tree[now].right!=-1) que.add(tree[now].right);
if(tree[now].left!=-1) que.add(tree[now].left);
}
return result;
}
public static String inOrder(int head,String result){
if(head==-1)
return result;
result=inOrder(tree[head].right,result);
result=result+head+" ";
result=inOrder(tree[head].left,result);
return result;
}
}
class InvertTree{
int left,right,value;
}

PAT-1102(Invert a Binary Tree)+二叉树的镜像+层次遍历+中序遍历+已知树的结构构树的更多相关文章

  1. PAT 1102 Invert a Binary Tree[比较简单]

    1102 Invert a Binary Tree(25 分) The following is from Max Howell @twitter: Google: 90% of our engine ...

  2. PAT 1102 Invert a Binary Tree

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

  3. 【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)

    题意: 输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历. AAAAAccepted code: #define HAVE_STR ...

  4. PAT甲级——1102 Invert a Binary Tree (层序遍历+中序遍历)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90577042 1102 Invert a Binary Tree ...

  5. 1102 Invert a Binary Tree——PAT甲级真题

    1102 Invert a Binary Tree The following is from Max Howell @twitter: Google: 90% of our engineers us ...

  6. PAT Advanced 1102 Invert a Binary Tree (25) [树的遍历]

    题目 The following is from Max Howell @twitter: Google: 90% of our engineers use the sofware you wrote ...

  7. 1102 Invert a Binary Tree (25 分)(二叉树遍历)

    二叉树有N个结点,给出每个结点的左右孩子结点的编号,把二叉树反转(左右孩子交换  所以是后序遍历交换) 输出反转后二叉树的层序遍历和中序遍历 #include<bits/stdc++.h> ...

  8. 1102. Invert a Binary Tree (25)

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

  9. PAT A1102 Invert a Binary Tree (25 分)——静态树,层序遍历,先序遍历,后序遍历

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

随机推荐

  1. 【uva 11082】Matrix Decompressing(图论--网络流最大流 Dinic+拆点二分图匹配)

    题意:有一个N行M列的正整数矩阵,输入N个前1~N行所有元素之和,以及M个前1~M列所有元素之和.要求找一个满足这些条件,并且矩阵中的元素都是1~20之间的正整数的矩阵.输入保证有解,而且1≤N,M≤ ...

  2. 【noi 2.6_9265】取数游戏(DP)

    题意:从自然数1到N中不取相邻2数地取走任意个数,问方案数. 解法:f[i][1]表示在前i个数中选了第i个的方案数,f[i][0]表示没有选第i个.f[i][1]=f[i-1][0];  f[i][ ...

  3. Codeforces Round #669 (Div. 2) B. Big Vova (枚举)

    题意:有一个长度为\(n\)的序列,你需要对其重新排序,构造一个新数组\(c\),\(c_{i}=gcd(a_{1},...,a{i})\)并且使得\(c\)的字典序最小. 题解:直接跑\(n\)次, ...

  4. Maven与Nexus3.x环境构建详解

    一.Maven介绍Apache Maven是一个创新的软件项目管理和综合工具.Maven提供了一个基于项目对象模型(POM)文件的新概念来管理项目的构建,可以从一个中心资料片管理项目构建,报告和文件. ...

  5. 查找命令中grep,find,which和whereis的使用及区别

    在linux系统中,许多时候需要查找某些文件或者字符,如果用ls, cd 等基础命令就显得很无力了,那么Linux提供了grep,find,which 三种查找命令,在这里我记录一下: 一.grep命 ...

  6. GPU 加速 & WebGL

    GPU 加速 & WebGL 开启 GPU 加速, 硬件加速 垃圾面试官,瞎忽悠 holy shit 美国想象力英语,前端 leader WebGL 加速 ??? 是什么鬼 ??? three ...

  7. how to input special keyboard symbol in macOS(⌘⇧⌃⌥)

    how to input special keyboard symbol in macOS(⌘⇧⌃⌥) emoji ctrl + command + space / ⌘⇧⌃ ⌘⇧⌃ Character ...

  8. free online code editor

    free online code editor online vscode https://stackblitz.com/ https://codesandbox.io/ https://codesh ...

  9. Flutter & APP & UI Components

    Flutter & APP & UI Components 下拉刷新或者上拉加载 https://github.com/OpenFlutter/flutter_screenutil h ...

  10. Flutter 学习路径

    Flutter 学习路径 docs https://flutter.dev/docs https://flutter.dev/community/china https://flutter-io.cn ...