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 ...
随机推荐
- 牛客编程巅峰赛S1第11场 - 黄金&钻石 C.牛牛找子集 (二分)
题意:有一\(n\)个数,从中找数构成相同的子集,要求子集元素个数为\(k\),求构成子集个数最多的情况,输出子集(字典序最小). 题解:我们可以对子集的个数二分答案,首先用桶记录每个元素的个数,然后 ...
- ArcGIS处理栅格数据(二)
五.栅格数据的配准 1.有参考图层 ① 在ArcMap里面添加需要配准的栅格数据集和参考数据集. ② 在ArcMap里面添加"地理配准"工具条. 添加成功后如下图所示: ③ 将需要 ...
- 1.初识Redis
作者 微信:tangy8080 电子邮箱:914661180@qq.com 更新时间:2019-08-14 20:35:36 星期三 欢迎您订阅和分享我的订阅号,订阅号内会不定期分享一些我自己学习过程 ...
- 2018ACM上海大都会赛 F Color it【基础的扫描线】
题目:戳这里 题意:有n*m个点全为白色,q个圆,将q个圆内所有的点都染成黑色,问最后剩下多少白色的点. 解题思路:每一行当做一个扫描线,扫描所有的圆,记录每一行在圆中的点即可,O(n*q). 附ac ...
- Linux 驱动框架---dm9000分析
前面学习了下Linux下的网络设备驱动程序的框架inux 驱动框架---net驱动框架,感觉知道了一个机器的大致结构还是不太清楚具体的细节处是怎么处理的,所以今天就来以dm9000这个网上教程最多的驱 ...
- making a resizable div effect in vanilla js
making a resizable div effect in vanilla js scroll image compare <!DOCTYPE html> <html lang ...
- X-Frame-Options & iframe & CORS
X-Frame-Options & iframe & CORS https://github.com/xgqfrms/FEIQA/issues/23 X-Frame-Options i ...
- website captcha
website captcha 验证码 hCaptcha hCaptcha通过询问对人类来说很容易且对机器来说很困难的简单问题,可以帮助您喜欢的Web服务阻止机器人,垃圾邮件和滥用行为. https: ...
- Dart 中断Future
更多 中断future 方法1) import 'package:async/async.dart'; void main() { var get = CancelableOperation.from ...
- Baccarat是如何运用去中心化治理模式的?
区块链的出现,让大家看到了去中心化的可能.去中心化的数字资产从最初的默默无闻,一路起起伏伏发展了十年,逐渐成为了大众认可的价值存储方式.去中心化的金融,使数字资产的生态建设者意识到,即使没有中心化的金 ...