当前位置:网站首页>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);
}

边栏推荐
- 自然语言处理系列(二)——使用RNN搭建字符级语言模型
- Deep understanding of NN in pytorch Embedding
- Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
- Post request body content cannot be retrieved repeatedly
- Leetcode14 最长公共前缀
- PyTorch nn. Full analysis of RNN parameters
- Performance tuning project case
- Yygh-9-make an appointment to place an order
- Codeforces 771 div2 B (no one FST, refers to himself)
- Take you ten days to easily finish the finale of go micro services (distributed transactions)
猜你喜欢

Deep understanding of P-R curve, ROC and AUC

Sort---

Natural language processing series (II) -- building character level language model using RNN

Dynamic debugging of multi file program x32dbg

Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law

PyTorch nn. Full analysis of RNN parameters

mysql索引和事务

drools动态增加、修改、删除规则

刷题---二叉树--2

机械臂速成小指南(七):机械臂位姿的描述方法
随机推荐
Take you ten days to easily finish the finale of go micro services (distributed transactions)
Leetcode14 longest public prefix
This article takes you to understand the operation of vim
MySQL与PostgreSQL抓取慢sql的方法
uniapp uni-list-item @click,uniapp uni-list-item带参数跳转
Log4j2
String palindrome hash template question o (1) judge whether the string is palindrome
字符串回文hash 模板题 O(1)判字符串是否回文
Mish shake the new successor of the deep learning relu activation function
Leetcode122 the best time to buy and sell stocks II
史上最易懂的f-string教程,收藏这一篇就够了
ThreadLocal的简单理解
【C语言】杨辉三角,自定义三角的行数
LeetCode—剑指 Offer 51. 数组中的逆序对
Pytorch builds LSTM to realize clothing classification (fashionmnist)
mysql表的增删改查(进阶)
自然语言处理系列(三)——LSTM
Leetcode209 subarray with the smallest length
计算二叉树的最大路径和
堆(優先級隊列)