https://pintia.cn/problem-sets/994805342720868352/problems/1038430130011897856

The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.

Given any two nodes in a binary tree, you are supposed to find their LCA.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers: M (≤ 1,000), the number of pairs of nodes to be tested; and N (≤10,000), the number of keys in the binary tree, respectively. In each of the following two lines, N distinct integers are given as the inorder and preorder traversal sequences of the binary tree, respectively. It is guaranteed that the binary tree can be uniquely determined by the input sequences. Then M lines follow, each contains a pair of integer keys U and V. All the keys are in the range of int.

Output Specification:

For each given pair of U and V, print in a line LCA of U and V is A. if the LCA is found and A is the key. But if A is one of U and V, print X is an ancestor of Y.where X is A and Y is the other node. If U or V is not found in the binary tree, print in a line ERROR: U is not found. or ERROR: V is not found. or ERROR: U and V are not found..

Sample Input:

6 8
7 2 3 4 6 5 1 8
5 3 7 2 6 4 8 1
2 6
8 1
7 9
12 -3
0 8
99 99

Sample Output:

LCA of 2 and 6 is 3.
8 is an ancestor of 1.
ERROR: 9 is not found.
ERROR: 12 and -3 are not found.
ERROR: 0 is not found.
ERROR: 99 and 99 are not found.

代码:

#include <bits/stdc++.h>
using namespace std; int N, M;
vector<int> in, pre;
map<int, int> pos; void lca(int inl, int inr, int preroot, int a, int b) {
if(inl > inr) return ;
int inroot = pos[pre[preroot]], ain = pos[a], bin = pos[b];
if(ain < inroot && bin < inroot)
lca(inl, inroot - 1, preroot + 1, a, b);
else if((ain > inroot && bin < inroot) || (ain < inroot && bin > inroot))
printf("LCA of %d and %d is %d.\n", a, b, in[inroot]);
else if(ain > inroot && bin > inroot)
lca(inroot + 1, inr, preroot + 1 + inroot - inl, a, b);
else if(ain == inroot)
printf("%d is an ancestor of %d.\n", a, b);
else if(bin == inroot)
printf("%d is an ancestor of %d.\n", b, a);
} int main() {
scanf("%d%d", &M, &N);
in.resize(N + 1), pre.resize(N + 1);
for(int i = 1; i <= N; i ++) {
scanf("%d", &in[i]);
pos[in[i]] = i;
}
for(int i = 1; i <= N; i ++)
scanf("%d", &pre[i]); for(int i = 0; i < M; i ++) {
int x, y;
scanf("%d%d", &x, &y);
if(pos[x] == 0 && pos[y] == 0)
printf("ERROR: %d and %d are not found.\n", x, y);
else if(pos[x] == 0 || pos[y] == 0)
printf("ERROR: %d is not found.\n", pos[x] == 0 ? x : y);
else
lca(1, N, 1, x, y);
}
return 0;
}  

LCA

FHFHFH

PAT 甲级 1151 LCA in a Binary Tree的更多相关文章

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

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

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

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

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

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

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

  7. 1151 LCA in a Binary Tree (30point(s))

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

  8. 【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)

    题意: 输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历. AAAAAccepted code: #define HAVE_STR ...

  9. PAT_A1151#LCA in a Binary Tree

    Source: PAT A1151 LCA in a Binary Tree (30 分) Description: The lowest common ancestor (LCA) of two n ...

随机推荐

  1. Echarts插件

    <%@ page contentType="text/html;charset=UTF-8" language="java" %><% Str ...

  2. 解决 spring-test 出现 Failed to load ApplicationContext 的异常

    在使用spring-test的时候,在启动@Test的方法时,spring-test会去加载spring的配置文件,这个时候如果配置文件没有在 @ContextConfiguration 中写全,就会 ...

  3. asp.net core结合NLog搭建ELK实时日志分析平台

    0.整体架构 整体架构目录:ASP.NET Core分布式项目实战-目录 一.介绍ELK 1.说明(此篇ELK采用rpm的方式安装在服务器上)-牛刀小试 承接上一篇文章的内容准备部署ELK来展示asp ...

  4. 最具有性价比的语言javascript之二,学习篇

    先祝福各位,国庆假期愉快!前面写了一篇 最具有性价比的语言javascript之介绍篇,引起了不少的争议. 有些人javascript吐槽,埋怨.我目前做的都是后台系统.每个人的工作经历和兴趣爱好不一 ...

  5. flask入门补充

    在上篇文章提到了json的编码问题.那么Flask是国外开发的框架,没有考虑到中文编码,那么我们就需要自己配置 那么在访问页面的时候会有 get 请求和post  请求.在下边我也会提到.以及没有接触 ...

  6. vue.js和vue-router和vuex快速上手知识

    vue.js和vue-router和vuex快速上手知识 一直以来,认为vue相比react而言,学习成本会更低,会更简单,但最近真正接触后,发现vue的各方面都有做一些客户化的优化,有一些亮点,但也 ...

  7. JavaScript学习笔记(七)—— 再说函数

    第八章 函数 1 函数声明和函数表达式 差别一:函数声明:函数在执行代码前被创建:函数表达式是在运行阶段执行代码时创建: 差别二:函数声明创建一个与函数同名的变量,并让她指向函数:使用函数表达式,不给 ...

  8. Tomcat源码学习(3)—— Digester介绍

    Digester方法详解: 通读Digester之前先分析下他的结构: 1.1该类继承了方法DefaultHandler2,DefaultHandler2继承了DefaultHandler是和sax解 ...

  9. markdown | Latex | 书写测试

    我永远喜欢markdown! 建图 graph TD; 1-->2 1-->3 1-->4 2-->5 2-->6 3-->7 3-->8 4-->9 ...

  10. [linux] 查看网卡UUID

    virtualbox复制了虚拟机,重新初始化网卡后,需要对/etc/sysconfig/network-scripts/ifcfg-eth0更新UUID值,虽然不改暂时也没发现有问题. 网上查找需要n ...