当前位置:网站首页>Find the common ancestor of any two numbers in a binary tree
Find the common ancestor of any two numbers in a binary tree
2022-07-02 12:13:00 【weixin_ fifty million one hundred and seventy-nine thousand nin】
The code is as follows :
void Anales(int arr[],int p, int q)
{
int pos = -1;
int size = 0;
int as[1] = { 0 };
int indexP = 0;
int indexQ = 0;
while (arr[size] != as[1])
{
if (arr[size] == p)
{
indexP = size;
}
if (arr[size] == q)
{
indexQ = size;
}
size++;
}
if (indexP > indexQ)
{
int subInt = indexP - indexQ; // For example, the interval between indexes is exactly equal to the smaller index , So their male // The common ancestor index is 0
if (subInt == indexQ)
{
pos = 0;
}
else
{
int fatherP = indexP;
while (fatherP > indexQ)
{
if (fatherP % 2) // If the current index is even , Its upper level = ( Current index - 2)/2
{
fatherP = (fatherP - 2) / 2;
}
else // If the current index is odd , Its upper level = ( Current index - 1)/2
{
fatherP = (fatherP - 1) / 2;
}
}
if (fatherP == indexQ)
{
pos = fatherP;
}
else
{
// At the same time, look for superiors until they are equal
while (fatherP != indexQ)
{
if (fatherP % 2)
{
fatherP = (fatherP - 2) / 2;
}
else
{
fatherP = (fatherP - 1) / 2;
}
if (indexQ % 2)
{
indexQ = (indexQ - 2) / 2;
}
else
{
indexQ = (indexQ - 1) / 2;
}
}
pos = indexQ;
}
}
}
else if (indexQ > indexP)
{
int subInt = indexQ - indexP;
if (subInt == indexP)
{
pos = 0;
}
else
{
while (indexQ > indexP)
{
if (indexQ % 2)
{
indexQ = (indexQ - 2) / 2;
}
else
{
indexQ = (indexQ - 1) / 2;
}
}
if (indexP == indexQ)
{
pos = indexQ;
}
else
{
while (indexP != indexQ)
{
if (indexP % 2)
{
indexP = (indexP - 2) / 2;
}
else
{
indexP = (indexP - 1) / 2;
}
if (indexQ % 2)
{
indexQ = (indexQ - 2) / 2;
}
else
{
indexQ = (indexQ - 1) / 2;
}
}
pos = indexQ;
}
}
}
else
{
pos = indexP;
}
std::cout << " Common ancestor :" << arr[pos] << "\t Indexes :" << pos << std::endl;
}
int main()
{
int arr[15] = { 3,5,1,6,2,0,8,0,0,7,4,0,0,0,0 };
Anales(arr,5,4);
}

边栏推荐
猜你喜欢

drools中then部分的写法

(C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

The differences and relationships among port, targetport, nodeport and containerport in kubenetes

堆(优先级队列)

(C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?

MSI announced that its motherboard products will cancel all paper accessories
![[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)](/img/18/f0c9ef6250a717f8e66c95da4de08c.jpg)
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)

Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator

Jenkins voucher management

conda常用命令汇总
随机推荐
Leetcode14 longest public prefix
PyTorch中repeat、tile与repeat_interleave的区别
小程序链接生成
Read the Flink source code and join Alibaba cloud Flink group..
YYGH-BUG-04
Natural language processing series (I) -- RNN Foundation
Applet link generation
Filtre de profondeur de la série svo2
堆(优先级队列)
Codeforces 771 div2 B (no one FST, refers to himself)
kubeadm join时出现错误:[ERROR Port-10250]: Port 10250 is in use [ERROR FileAvailable--etc-kubernetes-pki
【C语言】十进制数转换成二进制数
SparkContext: Error initializing SparkContext解决方法
Le tutoriel F - String le plus facile à comprendre de l'histoire.
浅谈sklearn中的数据预处理
conda常用命令汇总
CDA数据分析——AARRR增长模型的介绍、使用
Orb-slam2 data sharing and transmission between different threads
自然语言处理系列(二)——使用RNN搭建字符级语言模型
Go学习笔记—基于Go的进程间通信