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】的更多相关文章

  1. PAT 甲级 1147 Heaps (30 分) (层序遍历,如何建树,后序输出,还有更简单的方法~)

    1147 Heaps (30 分)   In computer science, a heap is a specialized tree-based data structure that sati ...

  2. PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****

    1057 Stack (30 分)   Stack is one of the most fundamental data structures, which is based on the prin ...

  3. pat 甲级 1057 Stack(30) (树状数组+二分)

    1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the princi ...

  4. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

  5. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  6. PAT甲级1127. ZigZagging on a Tree

    PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...

  7. PAT甲级1119. Pre- and Post-order Traversals

    PAT甲级1119. Pre- and Post-order Traversals 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通 ...

  8. PAT甲级1057. Stack

    PAT甲级1057. Stack 题意: 堆栈是最基础的数据结构之一,它基于"先进先出"(LIFO)的原理.基本操作包括Push(将元素插入顶部位置)和Pop(删除顶部元素).现在 ...

  9. PAT甲级1026. Table Tennis

    PAT甲级1026. Table Tennis 题意: 乒乓球俱乐部有N张桌子供公众使用.表的编号从1到N.对于任何一对玩家,如果有一些表在到达时打开,它们将被分配给具有最小数字的可用表.如果所有的表 ...

随机推荐

  1. Html5 学习笔记 --》布局

    不推荐: 浮动布局: footer 设置 clear : both 清理浮动 |  header            |  |边 |      | |内    |            内容     ...

  2. kaggle-制作评分卡

    https://blog.csdn.net/zpxcod007/article/details/80118580 制作A卡,申请评分卡 数据集:15万个样本,特征 主要预处理手段:缺失值,异常值,样本 ...

  3. 工控PLC中,关于定时器TON,TOF,的一点新认知,或者说醒悟吧!

    PLC  中的定时器,都是放在一个具体PRG任务单元中的,而PRG单元需要放在具体固定的周期循环任务中才能被执行,而这个周期循环任务的循环周期 T: 与定时器的定时时间T0:    T与T0 的数量级 ...

  4. java反射(二)--反射应用案例

    一.反射实例化对象 经过一系列的分析之后发现虽然可以获取Class类的实例化对象,但是依然觉得这个对象的获取意义不是很大,因此可以通过以下几个案例去理解反射的核心意义--反射实例化对象:获取Class ...

  5. 【转】C++ STL中常见容器的时间复杂度

    map, set, multimap, and multiset 上述四种容器采用红黑树实现,红黑树是平衡二叉树的一种.不同操作的时间复杂度近似为: 插入: O(logN) 查看:O(logN) 删除 ...

  6. java树的遍历

    java二叉树的遍历算法: http://blog.sina.com.cn/s/blog_70600f720100ujnp.html

  7. js转换成布尔类型boolean

    /** * js转换成布尔值 * a.转换方法:Boolean(var) * b.数字转换成布尔,除了0与NaN,其余都是true * c.字符串转换成布尔,除了空串"",其余都是 ...

  8. java synchronized的四种用法

    一 修饰方法 Synchronized修饰一个方法很简单,就是在方法的前面加synchronized,synchronized修饰方法和修饰一个代码块类似,只是作用范围不一样,修饰代码块是大括号括起来 ...

  9. 一、bootstrap-select输入选择框

    一.bootstrap-select简单使用 <!DOCTYPE html> <html lang="en"> <head> <meta ...

  10. CentOS 6.5安装aria2(转载)

    CentOS 6.5安装aria2 由于yum install aria2无法找到安装包,试了好几个源,都找不到,于是自己找了一些地址: 1.下载安装包: # wget http://ftp.tu-c ...