当前位置:网站首页>寻找二叉树中任意两个数的公共祖先
寻找二叉树中任意两个数的公共祖先
2022-07-02 09:43:00 【weixin_50179990】
代码如下:
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; //如索引的间隔正好等于更小的那个索引,那么它们的公//共祖先索引为0
if (subInt == indexQ)
{
pos = 0;
}
else
{
int fatherP = indexP;
while (fatherP > indexQ)
{
if (fatherP % 2) //如果当前索引为偶数,它的上一级 = (当前索引 - 2)/2
{
fatherP = (fatherP - 2) / 2;
}
else //如果当前索引为奇数,它的上一级 = (当前索引 - 1)/2
{
fatherP = (fatherP - 1) / 2;
}
}
if (fatherP == indexQ)
{
pos = fatherP;
}
else
{
//同时寻找上级直到相等
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 << "公共祖先:" << arr[pos] << "\t 索引:" << 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);
}

边栏推荐
- 【C语言】杨辉三角,自定义三角的行数
- CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
- PyTorch搭建LSTM实现服装分类(FashionMNIST)
- Input a three digit number and output its single digit, ten digit and hundred digit.
- Deep understanding of P-R curve, ROC and AUC
- 基于Arduino和ESP8266的连接手机热点实验(成功)
- 【C语言】十进制数转换成二进制数
- How does Premiere (PR) import the preset mogrt template?
- HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
- [geek challenge 2019] upload
猜你喜欢

Brush questions --- binary tree --2

How to Easily Create Barplots with Error Bars in R

BEAUTIFUL GGPLOT VENN DIAGRAM WITH R

CDA数据分析——AARRR增长模型的介绍、使用

Read the Flink source code and join Alibaba cloud Flink group..

5g era, learning audio and video development, a super hot audio and video advanced development and learning classic

(C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?
![[geek challenge 2019] upload](/img/04/731323142161a4994c14fedae38b81.jpg)
[geek challenge 2019] upload

【C语言】十进制数转换成二进制数

【工控老马】西门子PLC Siemens PLC TCP协议详解
随机推荐
Yygh-10-wechat payment
HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
Leetcode739 每日温度
mysql数据库基础
初始JDBC 编程
Maximum profit of jz63 shares
[C language] convert decimal numbers to binary numbers
Repeat, tile and repeat in pytorch_ The difference between interleave
Some problems encountered in introducing lvgl into esp32 Arduino
GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
Read the Flink source code and join Alibaba cloud Flink group..
字符串回文hash 模板题 O(1)判字符串是否回文
CDA data analysis -- Introduction and use of aarrr growth model
Depth filter of SvO2 series
K-Means Clustering Visualization in R: Step By Step Guide
小程序链接生成
jenkins 凭证管理
Dynamic memory (advanced 4)
post请求体内容无法重复获取