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. AtCoder Beginner Contest 170

    比赛链接:https://atcoder.jp/contests/abc170 A - Five Variables 题意 $5$ 个数中有 $1$ 个 $0$,判断是第几个. 代码 #include ...

  2. UVA1401 Remember the Word 字典树维护dp

    题目链接:https://vjudge.net/problem/UVA-1401 题目: Neal is very curious about combinatorial problems, and ...

  3. C - Last Digit

    Description The function f(n, k) is defined by f(n, k) = 1k + 2k + 3k +...+ nk. If you know the valu ...

  4. Educational Codeforces Round 69 (Rated for Div. 2) C. Array Splitting (思维)

    题意:给你一个长度为\(n\)的升序序列,将这个序列分成\(k\)段,每一段的值为最大值和最小值的差,求\(k\)段值的最小和. 题解:其实每一段的最大值和最小值的差,其实就是这段元素的差分和,因为是 ...

  5. js--执行上下文和作用域相关问题

    前言 如果你是或者你想成为一名合格的前端开发工作者,你必须知道JavaScript代码在执行过程,知道执行上下文.作用域.变量提升等相关概念,并且熟练应用到自己的代码中.本文参考了你不知道的JavaS ...

  6. CF1475-C. Ball in Berland

    CF1475-C. Ball in Berland 题意: 一个班级有\(a\)个男生和\(b\)个女生,现在这个班级有\(k\)对男女愿意一起出席毕业典礼,这里注意\(k\)对男女中可能会有某个男生 ...

  7. hdu 4497 GCD and LCM (非原创)

    GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  8. 输入函数input()、运算符

    一.input()函数的基本使用 present = input('大圣想要什么礼物') 作用:接受来自用户的输入 返回值类型:输入值的类型为str 值的存储:使用 = 对输入的值进行存储 name= ...

  9. webpack4.0源码解析之esModule打包分析

    入口文件index.js采用esModule方式导入模块文件,非入口文件index1.js分别采用CommonJS和esmodule规范进行导出. 首先,init之后创建一个简单的webpack基本的 ...

  10. mongodb & vue & node.js

    mongodb mongodb & vue & node.js https://docs.mongodb.com/manual/tutorial/install-mongodb-on- ...