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

边栏推荐
- mysql数据库基础
- 还不会安装WSL 2?看这一篇文章就够了
- 使用Sqoop把ADS层数据导出到MySQL
- Those logs in MySQL
- How to Add P-Values onto Horizontal GGPLOTS
- How does Premiere (PR) import the preset mogrt template?
- 基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)
- The most understandable f-string tutorial in history, collecting this one is enough
- Jenkins用户权限管理
- post请求体内容无法重复获取
猜你喜欢

Sort---

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

自然语言处理系列(二)——使用RNN搭建字符级语言模型

使用Sqoop把ADS层数据导出到MySQL

How to Create a Beautiful Plots in R with Summary Statistics Labels

Heap (priority queue)

Tas (file d'attente prioritaire)

堆(優先級隊列)

HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE

PyTorch搭建LSTM实现服装分类(FashionMNIST)
随机推荐
PHP 2D and multidimensional arrays are out of order, PHP_ PHP scrambles a simple example of a two-dimensional array and a multi-dimensional array. The shuffle function in PHP can only scramble one-dim
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT
CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
PgSQL string is converted to array and associated with other tables, which are displayed in the original order after matching and splicing
Heap (priority queue)
(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?
倍增 LCA(最近公共祖先)
Leetcode922 按奇偶排序数组 II
H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
b格高且好看的代码片段分享图片生成
xss-labs-master靶场环境搭建与1-6关解题思路
Leetcode122 买卖股票的最佳时机 II
mysql索引和事务
排序---
PyTorch nn. Full analysis of RNN parameters
堆(優先級隊列)
WSL 2 will not be installed yet? It's enough to read this article
Leetcode topic [array] -540- single element in an ordered array
Repeat, tile and repeat in pytorch_ The difference between interleave