当前位置:网站首页>寻找二叉树中任意两个数的公共祖先
寻找二叉树中任意两个数的公共祖先
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);
}

边栏推荐
- Differences between nodes and sharding in ES cluster
- 深入理解PyTorch中的nn.Embedding
- Fabric. JS 3 APIs to set canvas width and height
- Deep understanding of NN in pytorch Embedding
- Leetcode922 sort array by parity II
- This article takes you to understand the operation of vim
- CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
- GGPlot Examples Best Reference
- Pytorch builds LSTM to realize clothing classification (fashionmnist)
- HOW TO ADD P-VALUES TO GGPLOT FACETS
猜你喜欢

CONDA common command summary

From scratch, develop a web office suite (3): mouse events

CDA data analysis -- Introduction and use of aarrr growth model

How does Premiere (PR) import the preset mogrt template?

xss-labs-master靶场环境搭建与1-6关解题思路

刷题---二叉树--2

排序---

HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R

ThreadLocal的简单理解

MSI announced that its motherboard products will cancel all paper accessories
随机推荐
drools执行String规则或执行某个规则文件
使用Sqoop把ADS层数据导出到MySQL
Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)
Yygh-9-make an appointment to place an order
JZ63 股票的最大利润
mysql索引和事务
Find the factorial of a positive integer within 16, that is, the class of n (0= < n < =16). Enter 1111 to exit.
GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
堆(優先級隊列)
自然语言处理系列(二)——使用RNN搭建字符级语言模型
(C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
to_ Bytes and from_ Bytes simple example
On data preprocessing in sklearn
【工控老马】西门子PLC Siemens PLC TCP协议详解
Maximum profit of jz63 shares
字符串回文hash 模板题 O(1)判字符串是否回文
CDA数据分析——AARRR增长模型的介绍、使用
How does Premiere (PR) import the preset mogrt template?
Natural language processing series (III) -- LSTM
ES集群中节点与分片的区别