当前位置:网站首页>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;
}
边栏推荐
- Matlab judges the number of same purchases
- Matlab two-dimensional array example (extract data)
- Vue returns to the previous page without refreshing the page / Vue caches the page
- 2021-07-15Caused by: org. quartz. ObjectAlreadyExistsException: Unable to store Job : ‘DEFAULT. TASK_ 1‘
- CCF call auction (full mark code + problem solving ideas + skill summary) 201412 - 3
- CCF access control system (Full Score code + problem solving idea) 201412-1
- How does hbuilder display in columns?
- JS array
- Solution cannot use a scalar value as an array
- 1 figure to explain the difference and connection between nodejs and JS
猜你喜欢

LIS error: this configuration section cannot be used in this path

Component communication mode
![[buuctf] [geek challenge 2019] secret file](/img/00/23bebd013eb4035555c0057725e3c4.jpg)
[buuctf] [geek challenge 2019] secret file

CCF sequence segmentation (Full Score code + problem solving idea) 201509 -1

1 figure to explain the difference and connection between nodejs and JS
[email protected][])"/>NoViableAltException([email protected][])

PS tip: the video frame to Layer command cannot be completed because dynamiclink is not available

The first dark spring cup dnuictf

Matlab function for limit, definite integral, first-order derivative, second-order derivative (classic examples)

Computer screenshot how to cut the mouse in
随机推荐
CCF string matching (Full Score code + problem solving ideas + skill summary) March 3, 2014
CCF access control system (Full Score code + problem solving idea) 201412-1
Basic learning notes of C language
@PathVariable
For loop and promise to solve the problem of concurrent callback
Working principle and fault treatment of cutting cylinder in CNC machining center
Lfi-rce without controllable documents
Clear the route cache in Vue
Querywrapper in mybaits plus
【BUUCTF】[GXYCTF2019]Ping Ping Ping1
V3_ Chrome extended Chinese translation document V3 directory
Ctfshow getting started with the web (ThinkPHP topic)
How does hbuilder display in columns?
How to get palindrome number in MATLAB (using fliplr function)
Non decreasing column
MV3 04_ Introducing Manifest V3
2021-08-07 native and package types
CCF elimination games (Full Score code + problem solving ideas + skill summary) February 2, 2015
How to use Alibaba Vector Icon
CCF image rotation (Full Score code + problem solving idea) 201503-01