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 ...
随机推荐
- gym101002K. Inversions (FFT)
题意:给定一个仅含有AB的字母串 如果i有一个B j有一个A 且j>i 会对F(j-i)产生贡献 求出所有发Fi 题解:好像是很裸的FFT B的分布可以看作一个多项式 同理A也可以 然后把B的位 ...
- gym100923C. Por Costel and Bujor (高斯消元)
题意:简化一下 就是解N个 系数矩阵一样 等式右边列矩阵不一样的方程组 题解:系数矩阵一样 为什么我却毫无办法???? 其实只要把等式右边的矩阵都排在后面就好了啊 就变成解一个N x 2N的方程组了 ...
- hdu2430Beans(单调队列)
Mr. Pote's shop sells beans now. He has N bags of beans in his warehouse, and he has numbered them ...
- Codeforces Round #304 (Div. 2) D - Soldier and Number Game
D. Soldier and Number Game time limit per test 3 seconds memory limit per test 256 megabytes input s ...
- Codeforces Round #479 (Div. 3) D. Divide by three, multiply by two (DFS)
题意:给你一个长度为\(n\)的序列\(a\).对它重新排列,使得\(a_{i+1}=a_{i}/3\)或\(a_{i+1}=2*a_{i}\).输出重新排列后的序列. 题解:经典DFS,遍历这个序列 ...
- 向Pycharm中导入第三方包 && 更改Pycharm上镜像源
一.Pycharm本身导包 下载成功会这个样子(如下图) 但是有时因为包的版本太高,代码运行出错,此时需要选中右下角的Specify version,然后选择想要的版本即可 如果还出错,那就在命令行下 ...
- Ancient Printer HDU - 3460 贪心+字典树
The contest is beginning! While preparing the contest, iSea wanted to print the teams' names separat ...
- Kubernets二进制安装(3)之准备签发证书环境
1.在mfyxw50机器上分别下载如下几个文件:cfssl.cfssl-json.cfssl-certinfo cfssl下载连接地址: https://pkg.cfssl.org/R1.2/cfss ...
- next v5升级到next v7需要注意的地方
title: next v5升级到next v7需要注意的地方 date: 2020-03-04 categories: web tags: [hexo,next] 大部分的设置都是一样的,但有一些细 ...
- C++ 指针 new delete int*与string
一 指针 string和int 都可以认为是四个字节sizeof(string)==4, string是个类,它的空间在堆动态分配 二.delete 只是释放空间 三.new 数组 int size= ...