当前位置:网站首页>1151 LCA in a binary tree (30 points)
1151 LCA in a binary tree (30 points)
2022-06-30 14:55:00 【Xue Dongjing】
maxmin
1151 LCA in a Binary Tree (30 branch )
title
Give the middle order and pre order sequences of binary tree , Give more n logarithm , Each pair has two numbers , Ask the deepest common ancestor of these two numbers .
Ideas
First, create a tree in the order of the first and middle , Then go to the tree to find the common ancestor of the two points .
Code
#include<stdio.h>
#include<vector>
#include<string.h>
#include<map>
using namespace std;
typedef struct node{
int data;
node *lchild,*rchild;
}node;
map<int,node*>mp;
int in[1000007],pre[1000007];
node* build(int inl,int inr,int prl,int prr)
{
if(inl>inr||prl>prr){
return NULL;
}
node* root=new node;
int temp,ky;
root->data=in[inl];
for(int i=prl;i<=prr;i++){
if(pre[i]==in[inl]){
temp=i;
break;
}
}
mp[in[inl]]=root;
ky=temp-prl;
root->lchild=build(inl+1,inl+ky,prl,temp-1);
root->rchild=build(inl+1+ky,inr,temp+1,prr);
return root;
}
node* lcd(node* root,node* a,node* b)
{
if(root==NULL|root==a||root==b){
return root;
}
node* l=lcd(root->lchild,a,b);
node* r=lcd(root->rchild,a,b);
if(l==NULL){
// If on both sides, the root node will be returned , The one on one side will return to the child node .
return r;
}
if(r==NULL){
return l;
}
return root;
}
int main()
{
int m,n,a,b;
node *root;
scanf("%d%d",&m,&n);
for(int i=0;i<n;i++){
scanf("%d",&pre[i]);
}
for(int i=0;i<n;i++){
scanf("%d",&in[i]);
}
root=build(0,n-1,0,n-1);
for(int i=0;i<m;i++){
scanf("%d%d",&a,&b);
if(!mp[a]&&!mp[b]){
printf("ERROR: %d and %d are not found.\n",a,b);
}else if(!mp[a]){
printf("ERROR: %d is not found.\n",a);
}else if(!mp[b]){
printf("ERROR: %d is not found.\n",b);
}else{
node* p=lcd(root,mp[a],mp[b]);
if(p->data==a){
printf("%d is an ancestor of %d.\n",a,b);
}else if(p->data==b){
printf("%d is an ancestor of %d.\n",b,a);
}else{
printf("LCA of %d and %d is %d.\n",a,b,p->data);
}
}
}
return 0;
}
边栏推荐
- Average and maximum values of MATLAB matrix
- 1134: Legal C identifier query
- Error $(...) size is not a function
- Bucket sorting (C language)
- [buuctf] [actf2020 freshman competition]include
- Solve the problem that codeblocks20.03 on win11 cannot run for the first time
- Basic requirements for tool use in NC machining of vertical machining center
- V3 02——What‘s new in Chrome extensions
- ThinkPHP v3.2 comment annotation injection write shell
- The kth largest element in the sorted array
猜你喜欢

August 24, 2021 deque queue and stack

PS dynamic drawing

Shift operator (detailed)

CCF window (Full Score code + problem solving idea) March 2, 2014

【BUUCTF】 Have Fun

How does hbuilder display in columns?

CCF date calculation (Full Score code + skill summary) February 2, 2015

Thinkphp5 log file contains trick
![[buuctf] [geek challenge 2019] secret file](/img/00/23bebd013eb4035555c0057725e3c4.jpg)
[buuctf] [geek challenge 2019] secret file

2021-05-12
随机推荐
ES6 notes
Svn password forgetting solution
Minimum covering substring of two pointers
文本匹配——【NAACL 2021】AugSBERT
How to program and process such parts?
1066 root of AVL tree (25 points)
For loop and promise to solve the problem of concurrent callback
Greedy two-dimensional array sorting
【BUUCTF】 EasySql
Programming exercises: whole point and circle (solution ideas and code implementation)
Matlab function for limit, definite integral, first-order derivative, second-order derivative (classic examples)
The first dark spring cup dnuictf
Att & CK red team evaluation field (I)
1137: encrypted medical record
Quick sort (C language)
DefCamp Capture the Flag (D-CTF) 2021-22 web
Matlab finds a prime number that is greater than a given integer and follows this integer
[untitled]
1131: genetic correlation
Steps for commissioning of vertical machining center