当前位置:网站首页>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;
}
边栏推荐
- CCF Z-scan (full mark code + problem solving ideas) 201412-2
- Querywrapper in mybaits plus
- IO interview questions
- Maximum area of islands searched
- Pseudocode writing specification
- Non decreasing column
- Detailed explanation of settimeout() and setinterval()
- Working principle and fault treatment of cutting cylinder in CNC machining center
- Double pointer letter matching
- 【BUUCTF】[GXYCTF2019]Ping Ping Ping1
猜你喜欢

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

Matlab construction operation example

DiceCTF - knock-knock

CCF image rotation (Full Score code + problem solving idea) 201503-01

Querywrapper in mybaits plus

CCF elimination games (Full Score code + problem solving ideas + skill summary) February 2, 2015

Determine the number of digits of an integer in MATLAB (one line of code)

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

Thinkphp5 log file contains trick

2021-07-14 mybaitsplus
随机推荐
CCF command line options (Full Score code + problem solving ideas + skill summary) March 3, 2014
Repair of incorrect deletion of win10 boot entry
Greedy two-dimensional array sorting
1066 root of AVL tree (25 points)
Lihongyi machine learning 2020 homework summary
Average and maximum values of MATLAB matrix
【BUUCTF】 EasySql
How to program and process such parts?
Maximum area of islands searched
数控加工中心打刀缸工作原理及故障处理
Matlab judges the number of same purchases
高精度CNC加工中心为什么会出现误差?这4个原因你要注意!
Sum of squares of two pointers
Matlab function for limit, definite integral, first-order derivative, second-order derivative (classic examples)
Double pointer letter matching
Add attributes to multimode
中信期货开户麻烦吗安全吗,期货开户手续费是多少,能优惠吗
浅析卧式加工中心上不规则台阶孔存在问题
立式加工中心的数控加工对刀具使用基本要求
2021-08-07 native and package types