题意: 给出一棵二叉搜索树的前序遍历,问结点a和b的共同最低祖先是谁

#include<iostream>
#include<map>
using namespace std;
int pre[10003];
int t=0;
void find(int a,int b,int e) {t=pre[0];for(int i=0; i<e; ++i)if(pre[i]>=a&&pre[i]<=b||pre[i]<=a&&pre[i]>=b) {t=pre[i];break;}
}
int main() {int n,m;cin>>n>>m;map<int,int>num;for(int i=0; i<m; ++i) {scanf("%d",pre+i);num[pre[i]]=i+1;}for(int i=0; i<n; ++i) {int a,b;cin>>a>>b;if(!num[a]&&!num[b])printf("ERROR: %d and %d are not found.\n",a,b);else if(!num[a])printf("ERROR: %d is not found.\n",a);else if(!num[b])printf("ERROR: %d is not found.\n",b);else {t=a;find(a,b,m-1);if(t==a)printf("%d is an ancestor of %d.\n",a,b);else if(t==b)printf("%d is an ancestor of %d.\n",b,a);else printf("LCA of %d and %d is %d.\n",a,b,t);}}return 0;
}

1143 Lowest Common Ancestor相关推荐

  1. 1143. Lowest Common Ancestor

    1143. Lowest Common Ancestor (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

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

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

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

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

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

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

  5. 1143 Lowest Common Ancestor 甲级

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

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

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

  7. 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 ...

  8. 235 Lowest Common Ancestor of a Binary Search Tree

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

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

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

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

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

最新文章

  1. tcmalloc mysql 缓存_Tcmalloc优化Mysql内存管理
  2. NYOJ 409 郁闷的C小加(三)
  3. 初学SSH 配置+错误总结
  4. JavaScript and Ruby in ABAP
  5. php全选按钮怎么写,PHP中的“全选”复选框,其中包含header.php
  6. 好程序员教程分析Vue学习笔记五
  7. git把当前修改提交合并到上一条提交如何操作
  8. 自动填充固定行数的 GridView (转)
  9. 计算机病毒论坛,【本人整合】电脑病毒样本包大合集
  10. 录音文件下载_如何免费下载受版权保护的收费音乐
  11. android多个单选框超格,福昕PDF阅读器打印时提示“打印机被意外删除了”怎么处理?...
  12. 解决Macbook Pro蓝牙不可用问题
  13. H5视频自动播放和循环播放
  14. ubuntu设置时间为utc标准时间
  15. 单应性(Homography)变换
  16. 【C语言进阶】指针(进阶2)
  17. bind()函数介绍
  18. 我的物联网项目(十五) 微服务业务拆分
  19. 软件架构发展历程分享
  20. 使用animate 两个div向中间移动合并成一块

热门文章

  1. 免费版企业级杀毒软件mcafee使用报告。
  2. 程序员的思维修炼--读书感悟
  3. php7关闭缓存、禁用缓存
  4. upgrade-insecure-requests
  5. 小马哥spring编程核心思想_《Spring Boot编程思想(核心篇)》小马哥著 PDF电子书下载...
  6. 修改Window的hosts文件提示“该文件被其他程序占用,无法修改问题”解决方案
  7. 像素测量工具_像素大厨PxCook for Mac(自动标注工具)中文免费版
  8. c语言switch优点,关于C语言switch
  9. Unity摄像机上下旋转镜头实现
  10. DEVC++安装后编程运行时老是提示“源文件未编译”