PAT-1102(Invert a Binary Tree)+二叉树的镜像+层次遍历+中序遍历+已知树的结构构树
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)+二叉树的镜像+层次遍历+中序遍历+已知树的结构构树的更多相关文章
- PAT 1102 Invert a Binary Tree[比较简单]
1102 Invert a Binary Tree(25 分) The following is from Max Howell @twitter: Google: 90% of our engine ...
- PAT 1102 Invert a Binary Tree
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- 【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)
题意: 输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历. AAAAAccepted code: #define HAVE_STR ...
- PAT甲级——1102 Invert a Binary Tree (层序遍历+中序遍历)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90577042 1102 Invert a Binary Tree ...
- 1102 Invert a Binary Tree——PAT甲级真题
1102 Invert a Binary Tree The following is from Max Howell @twitter: Google: 90% of our engineers us ...
- 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 ...
- 1102 Invert a Binary Tree (25 分)(二叉树遍历)
二叉树有N个结点,给出每个结点的左右孩子结点的编号,把二叉树反转(左右孩子交换 所以是后序遍历交换) 输出反转后二叉树的层序遍历和中序遍历 #include<bits/stdc++.h> ...
- 1102. Invert a Binary Tree (25)
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- PAT A1102 Invert a Binary Tree (25 分)——静态树,层序遍历,先序遍历,后序遍历
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
随机推荐
- 【hdu 4859】海岸线(图论--网络流最小割)
题意:有一个区域,有'.'的陆地,'D'的深海域,'E'的浅海域.其中浅海域可以填充为陆地.这里的陆地区域不联通,并且整个地图都处在海洋之中.问填充一定浅海域之后所有岛屿的最长的海岸线之和. 解法:最 ...
- AtCoder Beginner Contest 188 E - Peddler (树)
题意:有\(n\)个点,\(m\)条单向边,保证每条边的起点小于终点,每个点都有权值,找到联通的点的两个点的最大差值. 题解:因为题目说了起点小于终点,所以我们可以反向存边,然后维护连通边的前缀最小值 ...
- Codeforces Round #577 (Div. 2) C. Maximum Median (模拟,中位数)
题意:给你一个长度为奇数\(n\)的序列.你可以对任意元素加上\(k\)次\(1\),求操作后的中位数最大. 题解:先对序列进行排序,然后对中位数相加,如果中位数和后面的元素相等,就对后面所有和当前中 ...
- 郁闷的出纳员 HYSBZ - 1503
OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的 工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常调整员工的工资.如果他 ...
- 爬虫——urllib.request包
一.引用包 import urllib.request 二.常用方法 (1)urllib.request.urlretrieve(网址,本地文件存储地址):直接下载网页到本地 urllib.reque ...
- CODE_TEST-- gtest
gtest 提供了一套优秀的 C++ 单元测试解决方案,简单易用,功能完善,非常适合在项目中使用以保证代码质量. https://blog.csdn.net/jcjc918/article/detai ...
- Leetcode(1)-两数之和
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], target ...
- js optional chaining operator
js optional chaining operator js 可选链 可选链操作符( ?. )允许读取位于连接对象链深处的属性的值,而不必明确验证链中的每个引用是否有效. ?. 操作符的功能类似于 ...
- Vue dynamic component All In One
Vue dynamic component All In One Vue 动态组件 vue 2.x https://vuejs.org/v2/guide/components-dynamic-asyn ...
- VSCode Plugin & Auto File Header Comments Generator
VSCode Plugin & Auto File Header Comments Generator Xcode SwiftUI // // ContentView.swift // Mem ...