当前位置:网站首页>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);
}
边栏推荐
- kubenetes中port、targetPort、nodePort、containerPort的区别与联系
- Jenkins用户权限管理
- LeetCode—剑指 Offer 37、38
- 排序---
- PyTorch nn. Full analysis of RNN parameters
- Leetcode122 the best time to buy and sell stocks II
- Leetcode922 sort array by parity II
- SVO2系列之深度滤波DepthFilter
- The most understandable f-string tutorial in history, collecting this one is enough
- 甜心教主:王心凌
猜你喜欢
Docker-compose配置Mysql,Redis,MongoDB
WSL 2 will not be installed yet? It's enough to read this article
Read the Flink source code and join Alibaba cloud Flink group..
YYGH-BUG-05
Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
[C language] convert decimal numbers to binary numbers
Jenkins user rights management
Mysql database foundation
The blink code based on Arduino and esp8266 runs successfully (including error analysis)
深入理解PyTorch中的nn.Embedding
随机推荐
lombok常用注解
使用Sqoop把ADS层数据导出到MySQL
二分刷题记录(洛谷题单)区间的甄别
考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
PyTorch中repeat、tile与repeat_interleave的区别
Leetcode122 买卖股票的最佳时机 II
Pytorch builds LSTM to realize clothing classification (fashionmnist)
小程序链接生成
This article takes you to understand the operation of vim
Le tutoriel F - String le plus facile à comprendre de l'histoire.
Discrimination of the interval of dichotomy question brushing record (Luogu question sheet)
全链路压测
Dynamic debugging of multi file program x32dbg
Leetcode209 长度最小的子数组
Full link voltage measurement
Find the factorial of a positive integer within 16, that is, the class of n (0= < n < =16). Enter 1111 to exit.
The blink code based on Arduino and esp8266 runs successfully (including error analysis)
SCM power supply
Day12 control flow if switch while do While guessing numbers game
[C language] Yang Hui triangle, customize the number of lines of the triangle