1143. Lowest Common Ancestor (30)

时间限制
200 ms

内存限制
65536 kB

代码长度限制
16000 B

判题程序
Standard

作者
CHEN, Yue

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 (<= 1000), the number of pairs of nodes to be tested; and N (<= 10000), 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.

题意就是按照要求找到BST树中U和V点的最近公共祖先 并按照要求输出出来

我们可以直接遍历整个表 由于BST的先序序列 就是元素的插入顺序 也就是元素的深度优先搜索的顺序 我们可以直接扫描整个表 就相当于深度搜索整棵树 如果当前点符合输出条件就停下输出

但是后来一直错不知道为什么 最后参考了网上的代码才发现 原来即使找到了两个点的LCA 也要按照输入顺序 先输出U 再输出V

哎。。。这一个错找了好久好久 PAT果然对输入输出一点也不友好。。。

#include<bits/stdc++.h>
using namespace std;
int a[10010];
map<int,bool>Map;
int main()
{int n,m;scanf("%d%d",&n,&m);for(int i=1;i<=m;i++){ scanf("%d",&a[i]);Map[a[i]]=1;}while(n--){int s,e;scanf("%d%d",&s,&e);bool fs=0,fe=0;fs = Map[s],fe = Map[e];if(fs==0&&fe==0)printf("ERROR: %d and %d are not found.\n",s,e);              else if(fs!=0&&fe==0)printf("ERROR: %d is not found.\n",e); else if(fs==0&&fe!=0)printf("ERROR: %d is not found.\n",s);        else{int ss = s,ee=e;s = min(ss,ee),e = max(ee,ss); for(int now = 1;now<=m;now++){if((s<a[now]&&e>a[now])){printf("LCA of %d and %d is %d.\n",ss,ee,a[now]);//就是这里输出如果是s,e 修改后的元素的输出 就会导致两个测试点出错break;}if(s==a[now]){printf("%d is an ancestor of %d.\n",s,e);break;}if(e==a[now]){printf("%d is an ancestor of %d.\n",e,s);break;}              } }}return 0;
}

1143. Lowest Common Ancestor相关推荐

  1. PAT甲级1143 Lowest Common Ancestor (30 分):[C++题解]LCA、最低公共祖先

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:二叉搜索树的中序遍历是隐含给定的,它的中序遍历就是从小到大排列. 所以这道题先是根据给定的前序遍历和中序遍历,建树. 建树的时候需要用 ...

  2. 【题解】【PAT甲】1143 Lowest Common Ancestor (30 分)(树)(BST)(最近公共祖先)

    题目链接 PTA | 程序设计类实验辅助教学平台 题目描述 The lowest common ancestor (LCA) of two nodes U and V in a tree is the ...

  3. 1143 Lowest Common Ancestor(建树与不建两种思路)

    目录 解法一 解法二 解法一 这题可以不建树,直接利用BST的性质:左子树<根节点<右子树,对先序序列进行遍历,如果有某个元素大于等于u,v中较小的且小于等于u,v中较大的,则可能是根节点 ...

  4. 1143 Lowest Common Ancestor 甲级

    题意: 给出一棵二叉搜索树的前序遍历,问结点u和v的共同最低祖先是谁,利用先序遍历特点. 二叉搜索树满足: 节点的左子树只包含键小于节点键的节点. 节点的键只包含节点的右键大于或等于子树的节点的键. ...

  5. 1143 Lowest Common Ancestor (30 分)【难度: 中 / 知识点: 最低公共祖先 未完成】

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

  6. Lowest Common Ancestor of a Binary Search Tree(树中两个结点的最低公共祖先)

    题目描述: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in ...

  7. 235 Lowest Common Ancestor of a Binary Search Tree

    题目 235 Lowest Common Ancestor of a Binary Search Tree 因为是binary search tree,因此利用没个节点的值进行二分查找即可复杂度O(h ...

  8. C语言二叉树的lowest common ancestor最低公共祖先(附完整源码)

    C语言二叉树的lowest common ancestor最低公共祖先 C语言二叉树的lowest common ancestor最低公共祖先完整源码(定义,实现,main函数测试) C语言二叉树的l ...

  9. C++lowest common ancestor最近公共祖先算法(附完整源码)

    C++lowest common ancestor最近公共祖先算法 C++lowest common ancestor最近公共祖先算法完整源码(定义,实现,main函数测试) C++lowest co ...

最新文章

  1. FFT镜头效果的新进展
  2. linux下普通用户更改密码原理(S权限)、SetUID
  3. rust的权限柜怎么做_潍坊装修知识~二胎家庭不做上下铺,把两张床靠墙放,中间做收纳柜,你感觉怎么样?...
  4. leetcode574. 当选者(SQL)
  5. JS 设计模式 一(接口)
  6. cmake release和debug代码运行效果不一样_这35个小细节,让你的Java 代码运行效率翻倍!...
  7. 自动生成宏程序软件_圆周等分孔钻孔宏程序计算器
  8. 【python 笔记】集合类型详解
  9. LOG. Supervisor基本使用
  10. 503 service unavailable php,503service unavailable错误提示解决方法
  11. 6/6 随机过程 马尔科夫链 习题(随机过程应用与模型第三章)
  12. VSCode远程控制服务器
  13. 用扫码枪收款钱到哪里_微信官方收款助手小程序商业版收款码入驻申请流程
  14. AppLocker绕过之路
  15. 会畅通讯会议客户端分析
  16. CSS实现幻灯片效果
  17. C++中的volatile(Primer读书笔记)
  18. 设计模式的七大原则之单一职责原则
  19. 马化腾——中国IT巨头之一
  20. 基于aws cloudfront的前端应用灰度方案

热门文章

  1. 写出gradle风格的groovy代码
  2. Java 集合系列14之 Map总结(HashMap, Hashtable, TreeMap, WeakHashMap等使用场景)
  3. 学习HTML:iframe用法总结收藏
  4. 如何在textarea中显示html代码
  5. Go语言通过odbc驱动连接华为高斯数据库
  6. uniapp添加网站favicon文件
  7. 10、jeecg 默认为空的字段值是如何被填充的?
  8. 微信支付宝扫一扫进入小程序的相关配置
  9. Math常用方法,String转float并且保留两位小数,除法
  10. android自定义控件几种,Android 自定义View一个控件搞定多种水波纹涟漪扩散效果 - CSDN博客...