当前位置:网站首页>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;
}
边栏推荐
- How to get palindrome number in MATLAB (using fliplr function)
- [buuctf] [actf2020 freshman competition]include
- Using member variables and member functions of a class
- Matlab judge palindrome number (only numbers)
- Detailed explanation of settimeout() and setinterval()
- After the MySQL service on the local computer is started and stopped, some services will automatically stop when they are not used by other services or programs
- Matlab draws the image of the larger function value of the two functions (super simple)
- 先锋期货安全么?现在期货开户都是哪些流程?期货手续费怎么降低?
- 2021-07-15Caused by: org. quartz. ObjectAlreadyExistsException: Unable to store Job : ‘DEFAULT. TASK_ 1‘
- [extensive reading of papers] a delicious recipe analysis framework for exploring multi modal recipes with variable attributes
猜你喜欢

Learn about data kinship JSON format design from sqlflow JSON format

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

CCF access control system (Full Score code + problem solving idea) 201412-1

August 24, 2021 deque queue and stack

Upgrade centos7 mysql5.5 to mysql5.7 non RPM in the form of tar package
![[extensive reading of papers] multimodal attribute extraction](/img/ec/546c107ac0d31deded7ca94fdf0e2d.jpg)
[extensive reading of papers] multimodal attribute extraction

Lihongyi machine learning 2020 homework summary
![[buuctf] [actf2020 freshman competition]exec1](/img/af/22051a5feb3c1f6d7201a483bde127.jpg)
[buuctf] [actf2020 freshman competition]exec1

CCF numerical sorting (Full Score code + problem solving ideas + skill summary) 201503-2

PS tip: the video frame to Layer command cannot be completed because dynamiclink is not available
随机推荐
Matlab to find prime pairs within 100
Solve the problem that codeblocks20.03 on win11 cannot run for the first time
[extensive reading of papers] a delicious recipe analysis framework for exploring multi modal recipes with variable attributes
[extensive reading of papers] multimodal attribute extraction
catkin_ Make reports an error, transfers the location of the workspace, and uses other people's workspace files to cause compilation errors
V3 01_ Welcome
2021-05-12
KnightCTF WEB
Programming exercises: whole point and circle (solution ideas and code implementation)
Text matching - [naacl 2022] GPL
Sum of CCF digits (full mark code + problem solving idea) 201512-1
K high frequency elements before sorting
Vue returns to the previous page without refreshing the page / Vue caches the page
Judgment of deep learning experiment results
Programming exercises: special numbers (problem solving ideas + code implementation)
In situ merging of two arrays with two pointers
Maximum area of islands searched
Matlab judges the number of same purchases
数控加工中心打刀缸工作原理及故障处理
1131: genetic correlation