1151LCA in a Binary Tree(最近公共祖先LCA)[通俗易懂]

1151LCA in a Binary Tree(最近公共祖先LCA)[通俗易懂]原题链接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.Given any two nodes in a binary tree, you are supposed to find their LCA.Input Specification:Each input file contains one test

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

原题链接

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.

Given any two nodes in a binary tree, 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 binary tree, respectively. In each of the following two lines, N distinct integers are given as the inorder and preorder traversal sequences of the binary tree, respectively. It is guaranteed that the binary tree can be uniquely determined by the input sequences. 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 binary tree, 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
7 2 3 4 6 5 1 8
5 3 7 2 6 4 8 1
2 6
8 1
7 9
12 -3
0 8
99 99

Sample Output:

LCA of 2 and 6 is 3.
8 is an ancestor of 1.
ERROR: 9 is not found.
ERROR: 12 and -3 are not found.
ERROR: 0 is not found.
ERROR: 99 and 99 are not found.

题解
最近公共祖先

#include<bits/stdc++.h>
#define x first
#define y second
#define send string::npos
#define lowbit(x) (x&(-x))
#define left(x) x<<1
#define right(x) x<<1|1
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
typedef struct Node * pnode;
const int N = 2e5 + 10;
const int M = 3 * N;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int Mod = 1e9;
int in[N],pre[N];
map<int,int>mm;
void lca(int a,int b,int l,int r,int ll,int rr){ 
   
    if(l <= r){ 
   
        int key = pre[l];
        int mid = mm[key];
        int lsum = mid - ll;
        int poxa = mm[a],poxb = mm[b];
// cout<<l<<" "<<r<<" "<<ll<<" "<<rr<<" "<<key<<"-"<<lsum<<endl;
        if(a == key)printf("%d is an ancestor of %d.\n",a,b);
        else if(b == key)printf("%d is an ancestor of %d.\n",b,a);
        else if(poxa < mid && poxb > mid || poxa > mid && poxb < mid)printf("LCA of %d and %d is %d.\n",a,b,key);
        else if(poxa < mid && poxb < mid)lca(a,b,l + 1,l + lsum,ll,mid - 1);
        else lca(a,b,l + lsum + 1,r,mid + 1,rr);
    }
}
int main(){ 
   
    int n,m;
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i = 0;i < m;i ++){ 
   
        cin>>in[i];
        mm[in[i]] = i;
    }
    for(int i = 0;i < m;i ++){ 
   
        cin>>pre[i];
    }
    int x,y;
    for(int i = 0;i < n;i ++)
    { 
   
        cin>>x>>y;
        bool flag1 = (mm.find(x) == mm.end() ? false:true);
        bool flag2 = (mm.find(y) == mm.end() ? false:true);
        if(!flag1 && !flag2)printf("ERROR: %d and %d are not found.\n",x,y);
        else if(!flag1)printf("ERROR: %d is not found.\n",x);
        else if(!flag2)printf("ERROR: %d is not found.\n",y);
        else lca(x,y,0,m-1,0,m-1);
    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/168772.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • sysbench简单使用

    sysbench简单使用

    2021年5月15日
    145
  • java tp_tp90和tp99指标

    java tp_tp90和tp99指标TP指标:TP50:指在一个时间段内(如5分钟),统计该方法每次调用所消耗的时间,并将这些时间按从小到大的顺序进行排序,取第50%的那个值作为TP50值;配置此监控指标对应的报警阀值后,需要保证在这个时间段内该方法所有调用的消耗时间至少有50%的值要小于此阀值,否则系统将会报警。TP90,TP99,TP999与TP50值计算方式一致,它们分别代表着对方法的不同性能要求,TP50相对较低,TP9…

    2025年7月3日
    2
  • 平分石头

    平分石头平分石头题目描述:有一n个石头,它们的重量分别是W1,…,Wn.写一个程序,把它们分成两堆,使得两堆石头的重量差最小。输入格式:输入n(1≤n≤20)表示石头的个数。第二行n个石头的重量W1,…,Wn(1≤Wi≤100000)输出格式:输入最小的差(绝对值)样例输入:5581327…

    2022年7月24日
    7
  • arp欺骗攻击原理_arp攻击的原理及防范

    arp欺骗攻击原理_arp攻击的原理及防范ARPSpoofing什么是ARP协议一台主机和另一台主机通信,要知道目标的IP地址,但是在局域网传输的网卡却不能直接识别IP地址,所以用APR解析协议将IP地址解析成MAC地址。ARP协议的基本功能就是通过目标设备的IP地址,来查询设备的MAC地址。在局域网的任意一台主机中,都有一个ARP缓存表,里面保存本机已知的此局域网中各主机和路由器的IP地址和MAC地址的对照表关系。ARP缓存表的生命周

    2022年9月29日
    3
  • 宝塔免费ssl_十四运小程序

    宝塔免费ssl_十四运小程序首先声明我也不是什么大佬,只是坑踩多了有经验了而已,看了无数大佬的文章教程,研究了大佬们的Github仓库说明,在此记录,方便自己,也方便大家,各位的点赞、收藏、关注是我持续更新的动力,写的内容也越来越多,如有侵权请私信我删除相关内容

    2022年10月14日
    2
  • CSS 颜色代码大全 CSS颜色对照表[通俗易懂]

    CSS 颜色代码大全 CSS颜色对照表[通俗易懂]转载地址:https://blog.csdn.net/u012117917/article/details/41604711HTML及CSS常用颜色英文词汇  黑色 银色 灰色 白色 茶色 红色 紫色 紫红 black silver gray white maroon …

    2022年5月17日
    142

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号