PAT-1151(LCA in a Binary Tree)+最近公共祖先+二叉树的中序遍历和前序遍历
LCA in a Binary Tree
PAT-1151
- 本题的困难在于如何在中序遍历和前序遍历已知的情况下找出两个结点的最近公共祖先。
- 可以利用据中序遍历和前序遍历构建树的思路,判断两个结点在根节点的左右子树,依次递归找到最近祖先
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
/**
* @Author WaleGarrett
* @Date 2020/9/5 16:56
*/
public class PAT_1151 {
static int[] preorder;
static int[] inorder;
static Map<Integer,Integer> map;//记录中序遍历数据的序号
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int m=scanner.nextInt();
int n=scanner.nextInt();
preorder=new int[n];
inorder=new int[n];
map=new HashMap<>();
for(int i=0;i<n;i++){
inorder[i]=scanner.nextInt();
map.put(inorder[i],i);
}
for(int i=0;i<n;i++){
preorder[i]=scanner.nextInt();
}
while(m!=0){
int u=scanner.nextInt(),v=scanner.nextInt();
//u和v表示待查找的两个结点
if(map.get(u)==null&&map.get(v)==null){
System.out.printf("ERROR: %d and %d are not found.\n",u,v);
}else if(map.get(u)==null||map.get(v)==null){
System.out.printf("ERROR: %d is not found.\n",map.get(u)==null?u:v);
} else LCA(0,n-1,0,u,v);
m--;
}
}
public static void LCA(int inl,int inr,int preroot,int a,int b){
if(inl>inr)
return;
int inroot=map.get(preorder[preroot]);//拿到将中序遍历序列一分为二的值
int pa=map.get(a),pb=map.get(b);
if(pa<inroot&&pb<inroot){
LCA(inl,inroot-1,preroot+1,a,b);
}else if(pa>inroot&&pb>inroot){
LCA(inroot+1,inr,preroot+inroot-inl+1,a,b);
}else if((pa<inroot&&pb>inroot)||(pa>inroot&&pb<inroot)){
System.out.printf("LCA of %d and %d is %d.\n",a,b,inorder[inroot]);
}else if(pa==inroot){
System.out.printf("%d is an ancestor of %d.\n",a,b);
}else if(pb==inroot){
System.out.printf("%d is an ancestor of %d.\n",b,a);
}
}
}
PAT-1151(LCA in a Binary Tree)+最近公共祖先+二叉树的中序遍历和前序遍历的更多相关文章
- PAT 1151 LCA in a Binary Tree[难][二叉树]
1151 LCA in a Binary Tree (30 分) The lowest common ancestor (LCA) of two nodes U and V in a tree is ...
- leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...
- 【PAT 甲级】1151 LCA in a Binary Tree (30 分)
题目描述 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has bo ...
- PAT 甲级 1151 LCA in a Binary Tree
https://pintia.cn/problem-sets/994805342720868352/problems/1038430130011897856 The lowest common anc ...
- PAT Advanced 1151 LCA in a Binary Tree (30) [树的遍历,LCA算法]
题目 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both ...
- PAT甲级|1151 LCA in a Binary Tree 先序中序遍历建树 lca
给定先序中序遍历的序列,可以确定一颗唯一的树 先序遍历第一个遍历到的是根,中序遍历确定左右子树 查结点a和结点b的最近公共祖先,简单lca思路: 1.如果a和b分别在当前根的左右子树,当前的根就是最近 ...
- PAT A1151 LCA in a Binary Tree (30 分)——二叉树,最小公共祖先(lca)
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
- 1151 LCA in a Binary Tree(30 分)
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
- 1151 LCA in a Binary Tree
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
随机推荐
- POJ - 1226 Substrings (后缀数组)
传送门:POJ - 1226 这个题跟POJ - 3294 和POJ - 3450 都是一样的思路,一种题型. POJ - 3294的题解可以见:https://www.cnblogs.com/li ...
- poj3693 Maximum repetition substring (后缀数组+rmq)
Description The repetition number of a string is defined as the maximum number R such that the strin ...
- 【poj 1182】食物链(图论--带权并查集)
题意:有3种动物A.B.C,形成一个"A吃B, B吃C,C吃A "的食物链.有一个人对N只这3类的动物有M种说法:第一种说法是"1 X Y",表示X和Y是同类. ...
- Codeforces Round #481 (Div. 3) G. Petya's Exams (贪心,模拟)
题意:你有\(n\)天的时间,这段时间中你有\(m\)长考试,\(s\)表示宣布考试的日期,\(d\)表示考试的时间,\(c\)表示需要准备时间,如果你不能准备好所有考试,输出\(-1\),否则输出你 ...
- Codeforces Round #653 (Div. 3) D. Zero Remainder Array (数学,模拟)
题意:有一组数,刚开始时\(x=0\),每次可以让\(x\)++或让某一个元素+=\(x\)后\(x\)++,每个元素只能加一次\(x\),问最少操作多少次使得所有元素能被\(k\)整除. 题解:每个 ...
- streamlink 安装使用
CentOS 安装: pip install streamlink 使用: #查看视频信息 streamlink $URL #下载视频 streamlink $URL best streamlink ...
- 金牛来到,福气来到——TcaplusDB新年放送
光阴似箭,2020转瞬间成为历史,牛年的钟声即将敲响,在此,TcaplusDB祝大家新的一年万事如意,牛年带给我们的福气,一定能让我们心想事成! 饮水思源,回顾过去的一年,我们深知,TcaplusDB ...
- 浅谈Webpack模块打包工具三
Source Map 生产代码与开发代码完全不同,如果需要调试应用的话会非常的麻烦,错误信息无法定位,Soutce Map就会逆向得到源代码, 须在打包之后的代码文件的末尾位置例如添加//# sour ...
- OpenStack Train版-8.安装neutron网络服务(控制节点)
安装neutron网络服务(controller控制节点192.168.0.10) 创建neutron数据库 mysql -uroot CREATE DATABASE neutron; GRANT A ...
- spark mllib als 参数
在一定范围内按照排列组合方式对rank,iterations,lambda进行交叉评估(根据均方根误差),找到最小误差的组合,用于建立矩阵分解模型.Signature: ALS.train( rati ...