题意:

给定前序遍历和中序遍历,问u和v的lca
(先是中序,后是中序)

题解:

方法一:

参考题解
将树映射到一颗BST上,在BST上找到答案然后再映射回原本的树
方法二:
参考题解
已知某个树的根结点,若a和b在根结点的左边,则a和b的最近公共祖先在当前子树根结点的左子树寻找,如果a和b在当前子树根结点的两边,在当前子树的根结点就是a和b的最近公共祖先,如果a和b在当前子树根结点的右边,则a和b的最近公共祖先就在当前子树的右子树寻找。中序加先序可以唯一确定一棵树,在不构建树的情况下,在每一层的递归中,可以得到树的根结点,在此时并入lca算法可以确定两个结点的公共祖先~

代码:

#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <cstdio>
#include <map>using namespace std;int m, n;
int opre[10009], oin[10009];
int pre[10009], in[10009];
map<int, int> otos, stoo;int main()
{cin >> m >> n;for (int i = 0; i < n; i++){cin >> oin[i];otos[oin[i]] = i;stoo[i] = oin[i];}for (int i = 0; i < n; i++){cin >> opre[i];pre[i] = otos[opre[i]];}for (int i = 0; i < m; i++){int u, v;int a;bool flag1 = true, flag2 = true;cin >> u >> v;if (otos.find(u) == otos.end())flag1 = false;if (otos.find(v) == otos.end())flag2 = false;if (!flag1 || !flag2){if (!flag1 && !flag2)printf("ERROR: %d and %d are not found.\n", u, v);elseprintf("ERROR: %d is not found.\n", flag1 == false ? u : v);continue;}u = otos[u];v = otos[v];for (int j = 0; j < n; j++){a = pre[j];if (a > u && a < v || a < u && a > v || a == u || a == v)break;}u = stoo[u];v = stoo[v];a = stoo[a];if (a == u || a == v)printf("%d is an ancestor of %d.\n", a, a == u ? v : u);elseprintf("LCA of %d and %d is %d.\n", u, v, a);}return 0;
}
#include <iostream>
#include <vector>
#include <map>
using namespace std;
map<int, int> pos;
vector<int> in, pre;
void lca(int inl, int inr, int preRoot, int a, int b) {if (inl > inr) return;int inRoot = pos[pre[preRoot]], aIn = pos[a], bIn = pos[b];if (aIn < inRoot && bIn < inRoot)lca(inl, inRoot-1, preRoot+1, a, b);else if ((aIn < inRoot && bIn > inRoot) || (aIn > inRoot && bIn < inRoot))printf("LCA of %d and %d is %d.\n", a, b, in[inRoot]);else if (aIn > inRoot && bIn > inRoot)lca(inRoot+1, inr, preRoot+1+(inRoot-inl), a, b);else if (aIn == inRoot)printf("%d is an ancestor of %d.\n", a, b);else if (bIn == inRoot)printf("%d is an ancestor of %d.\n", b, a);
}
int main() {int m, n, a, b;scanf("%d %d", &m, &n);in.resize(n + 1), pre.resize(n + 1);for (int i = 1; i <= n; i++) {scanf("%d", &in[i]);pos[in[i]] = i;}for (int i = 1; i <= n; i++) scanf("%d", &pre[i]);for (int i = 0; i < m; i++) {scanf("%d %d", &a, &b);if (pos[a] == 0 && pos[b] == 0)printf("ERROR: %d and %d are not found.\n", a, b);else if (pos[a] == 0 || pos[b] == 0)printf("ERROR: %d is not found.\n", pos[a] == 0 ? a : b);elselca(1, n, 1, a, b);}return 0;
}

1151 LCA in a Binary Tree 甲级相关推荐

  1. PAT甲级1151 LCA in a Binary Tree (30 分):[C++题解]LCA、最低公共祖先、哈希表映射

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 和下面这道题几乎是同一题:PAT甲级1143 Lowest Common Ancestor (30 分):[C++题解]LCA.最低 ...

  2. PAT甲级 1151 LCA in a Binary Tree (30分) LCA算法/C++

    1151 LCA in a Binary Tree (30分) 题目大意:给出一棵树的中序和先序遍历,找到这棵树中U和V最小的共同祖先. Tips: 使用一个unordered_map记录中序遍历的值 ...

  3. 1151 LCA in a Binary Tree (含求LCA的通法)

    目录 解法一思路 结果 解法一改进 解法一改进结果 ​解法二思路 解法一代码 解法一改进代码 解法二代码(AC) 解法一思路 1. 根据先序和中序建树 2. 对树进行深度优先遍历,找到每一个结点的父节 ...

  4. PAT甲组1151 LCA in a Binary Tree思路解析和代码

    A1151 题目链接 个人思路 题意 给出中序前序,找出一对测试节点的最深公共根节点 思路 与1143 Lowest Common Ancestor类似,但思考起来要比1143难,本题给出的是中序和前 ...

  5. 1151 LCA in a Binary Tree (30 分)【难度: 难 / 知识点: LCA 未完成】

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

  6. 1110 Complete Binary Tree(甲级)

    1110 Complete Binary Tree (25分) Given a tree, you are supposed to tell if it is a complete binary tr ...

  7. 1102 Invert a Binary Tree(甲级)

    1102 Invert a Binary Tree (25分) The following is from Max Howell @twitter: Google: 90% of our engine ...

  8. PAT甲级1110 Complete Binary Tree:[C++题解]判断完全二叉树

    文章目录 题目分析 题目链接 题目分析 分析: 按照层序的顺序将完全二叉树存在下标从1开始的数组中.如果是完全二叉树,会将数组中1 ~ n这些位置填满,最大下标就是n,如果最大下标大于n,说明中间有空 ...

  9. PAT甲级1102 Invert a Binary Tree:[C++题解]反转二叉树、递归

    文章目录 题目分析 题目链接 题目分析 反转二叉树这道题目特别出名!!!,是因为Homebrew这款Mac上大火的软件的作者到google面试,就考了这道题.面试官说了下面这段话:你竟然连在白板上翻转 ...

最新文章

  1. Jquery前端分页插件pagination同步加载和异步加载
  2. Mycat源码中的单例模式
  3. 看完这篇,你还不能理解 ‘数据库架构’?趁早回家吧
  4. npm install的代理问题
  5. .NET Core实用技巧(一)如何将EF Core生成的SQL语句显示在控制台中
  6. Github作为maven私服仓库用
  7. Unity3D-C#脚本介绍
  8. 【当头棒喝】你是真的了解云计算吗?
  9. 为什么时间是60进制的啊? ? ?
  10. 专访OneAPM创始人何晓阳:APM将是开发者必备服务
  11. EWM 过账期间修改(Posting only possible in periods***)
  12. 谷歌浏览器翻译本地的html,谷歌Chrome浏览器开启自带的翻译功能的方法
  13. 美式口语发音技巧:《英美发音区别》
  14. Android 第三方SDK的检测与提取
  15. html用css完成动画效果图,利用CSS Sprite实现PNG图片动画
  16. 安徽省2019c语言二级答案,安徽省计算机等级二级考试真题C语言.doc
  17. 如何训练出专属的 OpenAI Five ?
  18. 基于YOLO目标检测及OpenCV实现的游戏代玩人工智能体(Auto Gaming Agent) [4]
  19. 各种 PNG图片压缩对比分析
  20. Spring常见面试题55道(附答案2023最新版)

热门文章

  1. 机器学习 vs 深度学习到底有啥区别,为什么更多人选择机器学习
  2. 近期GitHub上最热门的开源项目(附链接)
  3. 【汇总推荐】深度学习、自然语言处理干货笔记汇总
  4. 简单六步,用数据说服你的听众
  5. 美国硅谷预测10年后的世界,再不懂你就落伍了
  6. 基于应用日志的扫描器检测实践
  7. dbeaver 数据转化 mapping_Python机器学习实例:数据竞赛-足球运动员身价估计
  8. 头文件定义全局变量_5.2 C++局部变量与全局变量 | 输出局部全局变量
  9. mysql从多个表查询数据类型_MySQL 之 多表查询
  10. mysql列增减_Mysql基本操作——增减改查