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. 2015ACM/ICPC亚洲区沈阳站-重现赛 M - Meeting (特殊建边,最短路)

    题意:有\(n\)个点,\(m\)个集合,集合\(E_i\)中的点都与集合中的其它点有一条边权为\(t_i\)的边,现在问第\(1\)个点和第\(n\)个点到某个点的路径最短,输出最短路径和目标点,如 ...

  2. js中for循环遍历的写法

    众所周知,for循环是编程中必不可少的知识点:那么如何高效的写出循环呢? 我们要先知道for循环的基础样式是由自有变量自增自减和if判组成的: 1 for(条件){ 2 执行语句 3 } 而for循环 ...

  3. Leetcode(4)-两个排序数组的中位数

      给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 示例 1: nums1 = [1, 3] ...

  4. Javascript实现"点按钮出随机背景色的"三个DIV

    <!DOCTYPE html> <html> <head> <title>Random_Color-Transformation</title&g ...

  5. 从GitHub Jobs! 看技术发展趋势! 程序员进阶必备!

    0. https://jobs.github.com/positions GitHub Jobs: 1. https://jobs.github.com/positions/38bb8dc8-b5b4 ...

  6. cocos2d-x & cocos2d-js

    cocos2d-x & cocos2d-js cocos2d-x new https://github.com/cocos2d/cocos2d-x cocos2d-x is a multi-p ...

  7. PIP & Python packages management

    PIP & Python packages management $ python3 --version # OR $ python3 -V # Python 3.7.3 $ pip --ve ...

  8. element ui 停止维护了

    ️‍♂️ element ui 停止维护了 最近看到有人说 element ui 已经停止维护了,还有点不相信; 不过到 github 验证一下,好像是真的呀 4 个月,没有任何更新了 https:/ ...

  9. SVG viewBox & coordinate system

    SVG viewBox & coordinate system https://codepen.io/xgqfrms/pen/abOOrjp <html> <body> ...

  10. qt 注册热键

    原文 将所需的库添加到您的qmake项目(.PRO文件) LIBS += \ -lUser32 2.在代码中包含所需的头文件. #include <windows.h> 在程序开始时注册热 ...