PAT甲级——A1143 LowestCommonAncestor【30】
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.
A binary search tree (BST) is recursively defined as a binary tree which has the following properties:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
- Both the left and right subtrees must also be binary search trees.
Given any two nodes in a BST, 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 BST, respectively. In the second line, N distinct integers are given as the preorder traversal sequence of the BST. 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 BST, 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
6 3 1 2 5 4 8 7
2 5
8 7
1 9
12 -3
0 8
99 99
Sample Output:
LCA of 2 and 5 is 3.
8 is an ancestor of 7.
ERROR: 9 is not found.
ERROR: 12 and -3 are not found.
ERROR: 0 is not found.
ERROR: 99 and 99 are not found.
Solution:
这道题给出一个重大的提示,就是SBT,题目说明是SBT不是让你自己去兴奋的去重建这棵树【我当时就是这么想的,也这样做了】,而是让你从中发现根节点与左右孩子节点的大小关系,然后从中找到突破口
我开始是重建了二叉树,然后DFS来找到两个节点的最低公共节点,然而。。。。。超时了
聪明的做法就是从前序遍历中找到突破口【我当时想了,但没有找到规律】
首先,使用map来记录哪些节点是存在的,用来判断不存在的节点
然后,遍历前序数组,当节点a ,与查询节点 u,v存在关系:(U<=a && a>=v)||(v<=a && u>=a), 那么u,v的最低公共节点就是a!!!!!
~~~~~~~~~~~
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int n, m;
vector<int>pre;
unordered_map<int, bool>map;
int main()
{
cin >> m >> n;
pre.resize(n);
for (int i = ; i < n; ++i)
{
cin >> pre[i];
map[pre[i]] = true;
}
while (m--)
{
int a, b;
cin >> a >> b;
if (map[a] != true && map[b] != true)
printf("ERROR: %d and %d are not found.\n", a, b);
else if (map[a] != true)
printf("ERROR: %d is not found.\n", a);
else if (map[b] != true)
printf("ERROR: %d is not found.\n", b);
else
{
int k = ;
for (k = ; k < n; ++k)
if (a <= pre[k] && pre[k] <= b || b <= pre[k] && pre[k] <= a)
break;
if (pre[k] == a)
printf("%d is an ancestor of %d.\n", a, b);
else if (pre[k] == b)
printf("%d is an ancestor of %d.\n", b, a);
else
printf("LCA of %d and %d is %d.\n", a, b, pre[k]);
}
}
return ;
}
PAT甲级——A1143 LowestCommonAncestor【30】的更多相关文章
- PAT 甲级 1147 Heaps (30 分) (层序遍历,如何建树,后序输出,还有更简单的方法~)
1147 Heaps (30 分) In computer science, a heap is a specialized tree-based data structure that sati ...
- PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the prin ...
- pat 甲级 1057 Stack(30) (树状数组+二分)
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the princi ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- PAT甲级1119. Pre- and Post-order Traversals
PAT甲级1119. Pre- and Post-order Traversals 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通 ...
- PAT甲级1057. Stack
PAT甲级1057. Stack 题意: 堆栈是最基础的数据结构之一,它基于"先进先出"(LIFO)的原理.基本操作包括Push(将元素插入顶部位置)和Pop(删除顶部元素).现在 ...
- PAT甲级1026. Table Tennis
PAT甲级1026. Table Tennis 题意: 乒乓球俱乐部有N张桌子供公众使用.表的编号从1到N.对于任何一对玩家,如果有一些表在到达时打开,它们将被分配给具有最小数字的可用表.如果所有的表 ...
随机推荐
- Linux系统CPU占用率较高问题排查思路
作为 Linux 运维工程师,在日常工作中我们会遇到 Linux服务器上出现CPU负载达到100%居高不下的情况,如果CPU 持续跑高,则会影响业务系统的正常运行,带来企业损失. 很多运维的同学遇到这 ...
- TP5数据库事务操作
使用事务处理的话,需要数据库引擎支持事务处理.比如 MySQL 的 MyISAM 不支持事务处理,需要使用 InnoDB 引擎. 使用 transaction 方法操作数据库事务,当发生异常会自动回滚 ...
- python 波波版压缩软件
#压缩软件 import os import zipfile import tkinter import tkinter.filedialog import tkinter.messagebox '' ...
- static_关键字
static关键字 1.在类中,用static声明的成员变量为静态成员变量,它为该类的公用变量,在第一次使用时被初始化,对于该类的所以对象来说,static成员变量只有一份. 2.用stati ...
- poj Drainage Ditches(最大流入门)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 85250 Accepted: 3316 ...
- android 查看网络图片
public class MainActivity extends Activity { private EditText pathText; private ImageView imageView; ...
- Jquery的$.ajax、$.get、$.post发送、接收JSON数据及回调函数用法
平时研究代码时,经常会遇到AJAX的相关用法,做项目时才真正体会到Ajax的强大之处(与服务器数据交互如此之便捷,更新DOM节点而不用刷新整个页面),以及运用的频繁程度.今天整理了一下自己之前没搞清楚 ...
- 【记录】linux 命令拷贝文件到远程服务器,linux下载文件到本地
Linux scp命令用于Linux之间复制文件和目录 -1 强制scp命令使用协议ssh1 -2 强制scp命令使用协议ssh2 -4 强制scp命令只使用IPv4寻址 -6 强制scp命令只使用I ...
- tomcat manager详解
Tomcat Manager是Tomcat自带的.用于对Tomcat自身以及部署在Tomcat上的应用进行管理的web应用.Tomcat是Java领域使用最广泛的服务器之一,因此Tomcat Mana ...
- SQL笔试题:下面是学生表(student)的结构说明
SQL笔试题:下面是学生表(student)的结构说明 SQL笔试题:下面是学生表(student)的结构说明 字段名称 字段解释 字段类型 字段长度 约束 s_id 学号 字符 10 PK s_na ...