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. Python异或加密字符串

    import os import sys import struct def enc(path, key): path_ret = "" for i in range(0, len ...

  2. 购物车2.0版——python第6天

    li = [{'}, {'}, {'}, {'}, {'}, ] shopping_car = {} # 定义购物车dict print('欢迎光临尚雅梦想旗舰店'.center(40)) # 先让顾 ...

  3. C# 与 C++ 互操作(C# 调用 C++ 的动态链接库)

    1.新建 C++ 动态链接库项目 CPlus.cpp: #include "stdafx.h" extern "C" __declspec(dllexport) ...

  4. SpringMVC学习(2):经典的HelloWorld实现

    前一篇简单介绍了Spring MVC的一些知识,下面就要开始学习如何把Spring MVC运用到具体的项目中去. 首先还是从一个简单的Hello World项目说起: 我机器的开发环境为: Ubunt ...

  5. 详细介绍如何计算两条折线的交点并使用Echarts展示以及图表优化

    1.背景 前段时间公司有个需求,需要在一个图表中展示两条折线,并且绘制出两条线的交点.为了满足需求大哥的需求,我也是着实想了有一会.下面我就把具体的实现过程给大家展示一下. 1.1.ECharts 简 ...

  6. onLaunch与onLoad同步获取用户数据

    前言 在开发项目的时候遇到从全局获取用户信息,逻辑是从app.js中的onLauch获取,page页面的onLoad拿到数据填充到页面.遇到的问题是onLauch与onLoad是异步的,没办法从页面判 ...

  7. Nginx的应用之动静分离

    Nginx 的动静分离 我们通过中间件将动态请求和静态请求进行分离,减少了不必要的请求消耗和延时. 动静分离后,即使动态服务不可用,但静态资源不会受到影响. 应用实例 1.准备环境 系统 角色 主机名 ...

  8. python安装pika模块rabbitmq

    1.pip install pika 2.如找不到 拷贝 D:\python\testmq\venv\Lib\site-packages  \pika目录

  9. mysql中级操作

    解析sql执行过程 show VARIABLES like '%profil%' //查看是否开启了剖析 如没开启set profiling=1; 启用 show profiles; set @que ...

  10. OAuth_2

    角色: OAuth2.0为用户和应用定义了如下角色: 资源拥有者.资源服务器.客户端应用.授权服务器 资源拥有者:拥有共享数据的人或应用,比如Facebook的用户就是 资源拥有者,但资源拥有者也可以 ...