PAT甲级-1151 LCA in a Binary Tree (30 分)
发布日期:2022-02-10 08:11:03 浏览次数:17 分类:技术文章

本文共 1933 字,大约阅读时间需要 6 分钟。

题目:
分析:一般二叉树的LCA,先根据中序和前序建立二叉树,然后进行查找,根据高度来判断:
  1. 把高度低的其中一个节点向上移动到与另外一个高度相同为止
  2. 若移动到该节点时,刚好为另外一个节点,那么这个节点就是另外一个节点的ancestor
  3. 否则两个节点继续同时向上,直到找到相同的父母为止
#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define MAX 100001#define MM 1020typedef long long ll;int n,m,k;int in[10001];int pre[10001];struct Node{ int key; Node *left; Node* right; Node *last; int height;};Node *create(int prel,int prer,int inl,int inr,int h){ if(prel>prer) return NULL; Node *root = (Node*)malloc(sizeof(Node)); int key = pre[prel]; root->key = key; root->height = h; int idx = 0; for(int i = 0;i<=inr;i++) { if(in[i] == key) { idx = i; break; } } int num = idx - inl; root->left = create(prel+1, prel+num, inl,idx-1,h+1); if(root->left!=NULL) root->left->last = root; root->right = create(prel+num+1,prer, idx+1,inr,h+1); if(root->right!=NULL) root->right->last = root; return root;}Node* get(Node*root,int x){ if(root==NULL) return NULL; if(x == root->key) return root; Node *a = get(root->left,x); if(a!=NULL) return a; a = get(root->right,x); if(a!=NULL) return a;}void mov(Node *root,int a,int b){ Node*x = get(root,a); Node*y = get(root,b); if(x == NULL && y == NULL) printf("ERROR: %d and %d are not found.\n",a,b); else if(x == NULL) printf("ERROR: %d is not found.\n",a); else if(y == NULL) printf("ERROR: %d is not found.\n",b); else{ while(x->height != y->height) { if(x->height>y->height) x = x->last; else y = y->last; } if(x == y) { if(x->key == a) printf("%d is an ancestor of %d.\n",a,b); else printf("%d is an ancestor of %d.\n",b,a); } else{ while(x!=y) { x = x->last; y = y->last; } printf("LCA of %d and %d is %d.\n",a,b,x->key); } }}int main(){ cin>>m>>n; for(int i = 0;i
>in[i]; for(int i = 0;i
>pre[i]; Node *root=NULL; root=create(0,n-1,0,n-1,0); while(m--) { int a,b; cin>>a>>b; mov(root,a,b); } return 0;}

转载地址:https://blog.csdn.net/qq_43567222/article/details/114388449 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:AtCoder - arc006_3
下一篇:CodeForces-Magic Numbers

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年03月30日 09时07分02秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

【Leetcode刷题篇】leetcode301 删除无效的括号 2019-04-26
【Leetcode刷题篇】leetcode239 滑动窗口最大值 2019-04-26
【Leetcode刷题篇】leetcode76 最小覆盖子串 2019-04-26
【Leetcode刷题篇】leetcode10 正则表达式匹配 2019-04-26
【Leetcode刷题篇】leetcode32 最长有效括号 2019-04-26
【Leetcode刷题篇】leetcode128 最长连续序列 2019-04-26
【Leetcode刷题篇】leetcode72 编辑距离 2019-04-26
【Leetcode刷题篇】leetcode312 戳气球 2019-04-26
前后端分离如何使用spring boot处理跨域请求 2019-04-26
【Leetcode刷题篇】leetcode283 移动零 2019-04-26
【Leetcode刷题篇】leetcode611 有效三角形的个数 2019-04-26
【Leetcode刷题篇】leetcode26 删除排序数组中的重复项 2019-04-26
【大话Java面试】-如何通俗易懂的理解Redis的分布式寻址算法hash slot? 2019-04-26
【大话Java面试】-如何通俗易懂的理解单例模式? 2019-04-26
【大话Java面试】请列出Java中几个常用的设计模式? 2019-04-26
【大话Java面试】-如何通俗易懂的理解Java异常以及Java异常处理? 2019-04-26
【大话Mysql面试】-Mysql的索引为什么要使用B+树,而不是B树,红黑树等之类? 2019-04-26
【大话Mysql面试】-如何通俗易懂的了解Mysql的索引最左前缀匹配原则 2019-04-26
【大话Mysql面试】-MYSQL的两种存储引擎MyISAM与InnoDB的区别是什么? 2019-04-26
【大话Mysql面试】-InnoDB可重复读隔离级别下如何避免幻读?MVCC和next-key是什么 2019-04-26