PAT 甲级 1151 LCA in a Binary Tree
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的更多相关文章
- PAT甲级|1151 LCA in a Binary Tree 先序中序遍历建树 lca
给定先序中序遍历的序列,可以确定一颗唯一的树 先序遍历第一个遍历到的是根,中序遍历确定左右子树 查结点a和结点b的最近公共祖先,简单lca思路: 1.如果a和b分别在当前根的左右子树,当前的根就是最近 ...
- 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[难][二叉树]
1151 LCA in a Binary Tree (30 分) The lowest common ancestor (LCA) of two nodes U and V in a tree is ...
- 【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 ...
- 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 ...
- 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 ...
- 【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)
题意: 输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历. AAAAAccepted code: #define HAVE_STR ...
- 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 ...
随机推荐
- mfc 类的const对象
知识点 类的const对象 const类的成员函数 一. 类的const对象 const 意谓着只读 意谓着所标记的类成员变量不成出现在=号的左边. 构造函数除外. ,,); //比如在存放出生日期的 ...
- Ubuntu14.04 64位机上安装OpenCV2.4.13(CUDA8.0)版操作步骤
Ubuntu14.04 64位机上安装CUDA8.0的操作步骤可以参考http://blog.csdn.net/fengbingchun/article/details/53840684,这里是在已经 ...
- ubuntu 图形化界面 gui 桌面版 root登录 sorry,that didn't work.please try again! 抱歉,认证失败。请重试
出现这种问题,用下面的方法就行了 https://jingyan.baidu.com/article/bad08e1e224b2709c85121f1.html 而且我发现,因为我用的是英文版的ubu ...
- 内置函数——eval、exec、compile
内置函数——eval.exec.compile eval() 将字符串类型的代码执行并返回结果 print(eval('1+2+3+4')) exec()将自字符串类型的代码执行 print(exec ...
- VMware vSphere 6.0 安装及管理手册
目录 1. VMWARE_VSPHERE安装 1.1. 底层ESXI 安装步骤 1.2. VCENTER安装步骤 1) 准备vCenter安装环境 2) vCenter安装步骤 2. VMWARE_V ...
- 1.Python3.6环境部署
标题:Python3.6环境部署文档 作者:刘耀 内容 Linux部署Python3.6环境 Mac部署Python3.6环境 Window10部署Python3.6环境 Pycharm安装 1. L ...
- tty命令详解
基础命令学习目录首页 原文链接:http://blog.chinaunix.net/uid-9525959-id-2001836.html [功能] 打印连接到标准输入的终端的文件名. [描述] ...
- 导出excel失败,提示提示加载类型库/DDL出错
首先,这里提供的解决办法仅适用于出现如下异常的情况:无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的 COM 对象强制转换为接口类型“M ...
- Spring的Controller映射规则
URL映射 1) 一般格式@RequestMapping(value=“/test”) 2) 可以使用模板模式映射,@RequestMapping(value=“/test/{userId}”) 3) ...
- OO第三阶段作业总结
调研: 最早的程序设计是直接采用机器语言来编写的,或者使用二进制码来表示机器能够识别和执行的指令和数据.机器语言的优点在于速度快,缺点在于写起来实在是太困难了,编程效率低,可读性差,并且 ...