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)+最近公共祖先+二叉树的中序遍历和前序遍历的更多相关文章

  1. 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 ...

  2. leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)

    题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...

  3. 【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 ...

  4. PAT 甲级 1151 LCA in a Binary Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/1038430130011897856 The lowest common anc ...

  5. 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 ...

  6. PAT甲级|1151 LCA in a Binary Tree 先序中序遍历建树 lca

    给定先序中序遍历的序列,可以确定一颗唯一的树 先序遍历第一个遍历到的是根,中序遍历确定左右子树 查结点a和结点b的最近公共祖先,简单lca思路: 1.如果a和b分别在当前根的左右子树,当前的根就是最近 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. P1268 树的重量(板子)

    题目: 题目描述 树可以用来表示物种之间的进化关系.一棵"进化树"是一个带边权的树,其叶节点表示一个物种,两个叶节点之间的距离表示两个物种的差异.现在,一个重要的问题是,根据物种之 ...

  2. Redis全面解析

    1.什么是Redis? Redis是BSD协议,是一个高性能的key-value非关系型数据库. 2.redis单线程问题 所谓的单线程指的是网络请求模块使用了一个线程(所以不需考虑并发安全性),即一 ...

  3. RuntimeError already started

    Env: os: Ubuntu python3 pytorch vscode Desc 在上述环境中运行A3C多进程模型,使用命令行时没问题,使用vscode时出现 'RuntimeError: al ...

  4. Django实现文件上传

    一.HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  5. 什么样的 SQL 不走索引

    参考: MySQL 索引优化全攻略 索引建立的规则 1.能创建唯一索引就创建唯一索引 2.为经常需要排序.分组和联合操作的字段建立索引 3.为常作为查询条件的字段建立索引 如果某个字段经常用来做查询条 ...

  6. 按层次顺序创建二叉树;判断BST

    https://github.com/TouwaErioH/subjects/tree/master/C%2B%2B/PA2 BST 假设已经给定树节点的结构不可修改. 然后输入是按照层次顺序 怎样创 ...

  7. os-hackNOS-2(wp5.3本地文件包含 rbash绕过)

    一.信息收集 直接netdiscover,找到IP是 192.168.56.101 然后端口扫描一波 只打开了22和80端口,访问一下80端口,是apache首页,那就继续查目录赛.,发现了一个tsw ...

  8. TS & ES-Next & playground

    TS & ES-Next & playground TS TypeScript: TS Playground - An online editor for exploring Type ...

  9. Django : Security in Django

    Security in Django https://docs.djangoproject.com/en/1.10/topics/security/ 1 Cross site scripting (X ...

  10. Android Jetpack All In One

    Android Jetpack All In One 在2018年,我们推出了Android Jetpack作为一组库,以帮助开发人员遵循最佳实践,减少样板代码以及编写可在Android版本和设备之间 ...