给定一个二叉树
作为其根源并为任何父母
它的左孩子将是2 * i ,右孩子将是2 * i + 1 。任务是找到两个节点n1和n2之间的最小距离。
1
/ \
2 3
/ \ / \
4 5 6 7
/ \ / \ / \ / \
. . . . . . . .
例子:
Input : n1 = 7, n2 = 10
Output : 5
Input : n1 = 6, n2 = 7
Output : 4
找到二叉树的两个给定节点之间的最小距离的方法有很多。
这是在给定节点的二进制表示的帮助下找到相同内容的有效方法。对于任何节点,请仔细查看其二进制表示形式。例如,考虑节点9 。 9的二进制表示形式是1001 。因此,要找到从根到节点的路径,请找到该节点的二进制表示形式,并在该二进制表示形式中从左向右移动,如果遇到1则移动到树中的右子节点,如果遇到0则移动到左子节点。 。
Path of 9 as per bit representation
1
/ \
2 3
/\ /\
4 5 6 7
/\
8 9.
因此,为了找到两个节点之间的最小距离,我们将尝试找到两个节点的二进制表示形式的公共部分,它实际上是从根到LCA的公共路径。让两个节点的前k位相同。同样,如果二进制形式为n位,则表明从根到该节点的路径距离为n长度。因此,根据以上陈述,我们可以轻松得出以下结论:如果m和n是两个节点的位长,并且两个节点的值的k个起始位相同,则两个节点之间的最小距离为: m + n – 2 * k 。
注意:最后类似的位显示LCA在给定的二叉树中的位置。
下面是上述方法的实现:
C++
// C++ program to find minimum distance between
// two nodes in binary tree
#include
using namespace std;
// Function to get minimum path distance
int minDistance(int n1, int n2)
{
/** find the 1st dis-similar bit **/
// count bit length of n1 and n2
int bitCount1 = floor(log2(n1)) + 1;
int bitCount2 = floor(log2(n2)) + 1;
// find bit difference and maxBit
int bitDiff = abs(bitCount1 - bitCount2);
int maxBitCount = max(bitCount1, bitCount2);
if (bitCount1 > bitCount2) {
n2 = n2 * pow(2, bitDiff);
}
else {
n1 = n1 * pow(2, bitDiff);
}
int xorValue = n1 ^ n2;
int bitCountXorValue;
if( xorValue == 0)
bitCountXorValue = 1;
else
{
bitCountXorValue = floor(log2(xorValue)) + 1;
}
int disSimilarBitPosition = maxBitCount -
bitCountXorValue;
// calculate result by formula
int result = bitCount1 + bitCount2 -
2 * disSimilarBitPosition;
return result;
}
// Driver program
int main()
{
int n1 = 12, n2 = 5;
cout << minDistance(n1, n2);
return 0;
}
Java
// Java program to find minimum distance between
// two nodes in binary tree
import java.util.*;
class GFG
{
// Function to get minimum path distance
static int minDistance(int n1, int n2)
{
/** find the 1st dis-similar bit **/
// count bit length of n1 and n2
int bitCount1 =(int) Math.floor((Math.log(n1) /
Math.log(2))) + 1;
int bitCount2 = (int)Math.floor((Math.log(n2) /
Math.log(2))) + 1;
// find bit difference and maxBit
int bitDiff = Math.abs(bitCount1 - bitCount2);
int maxBitCount = Math.max(bitCount1, bitCount2);
if (bitCount1 > bitCount2)
{
n2 = n2 *(int) Math.pow(2, bitDiff);
}
else
{
n1 = n1 *(int) Math.pow(2, bitDiff);
}
int xorValue = n1 ^ n2;
int bitCountXorValue;
if( xorValue == 0)
bitCountXorValue = 1;
else
{
bitCountXorValue = (int)Math.floor((Math.log(xorValue) /
Math.log(2))) + 1;
}
int disSimilarBitPosition = maxBitCount -
bitCountXorValue;
// calculate result by formula
int result = bitCount1 + bitCount2 - 2 * disSimilarBitPosition;
return result;
}
// Driver program
public static void main(String args[])
{
int n1 = 12, n2 = 5;
System.out.println(minDistance(n1, n2));
}
}
// This code is contributed by
// Sanjit_Prasad
Python3
# Python 3 program to find minimum distance
# between two nodes in binary tree
from math import log2
# Function to get minimum path distance
def minDistance(n1, n2):
# find the 1st dis-similar bit
# count bit length of n1 and n2
bitCount1 = int(log2(n1)) + 1
bitCount2 = int(log2(n2)) + 1
# find bit difference and maxBit
bitDiff = abs(bitCount1 - bitCount2)
maxBitCount = max(bitCount1, bitCount2)
if (bitCount1 > bitCount2):
n2 = int(n2 * pow(2, bitDiff))
else:
n1 = int(n1 * pow(2, bitDiff))
xorValue = n1 ^ n2
if xorValue == 0:
bitCountXorValue = 1
else:
bitCountXorValue = int(log2(xorValue)) + 1
disSimilarBitPosition = (maxBitCount -
bitCountXorValue)
# calculate result by formula
result = (bitCount1 + bitCount2 - 2 *
disSimilarBitPosition)
return result
# Driver Code
if __name__ == '__main__':
n1 = 12
n2 = 5
print(minDistance(n1, n2))
# This code is contributed by
# Surendra_Gangwar
C#
// C# program to find minimum distance between
// two nodes in binary tree
using System;
class GFG
{
// Function to get minimum path distance
static int minDistance(int n1, int n2)
{
/** find the 1st dis-similar bit **/
// count bit length of n1 and n2
int bitCount1 =(int) Math.Floor((Math.Log(n1) /
Math.Log(2))) + 1;
int bitCount2 = (int)Math.Floor((Math.Log(n2) /
Math.Log(2))) + 1;
// find bit difference and maxBit
int bitDiff = Math.Abs(bitCount1 - bitCount2);
int maxBitCount = Math.Max(bitCount1, bitCount2);
if (bitCount1 > bitCount2)
{
n2 = n2 *(int) Math.Pow(2, bitDiff);
}
else
{
n1 = n1 *(int) Math.Pow(2, bitDiff);
}
int xorValue = n1 ^ n2;
int bitCountXorValue;
if( xorValue == 0)
bitCountXorValue = 1;
else
{
bitCountXorValue = (int)Math.Floor((Math.Log(xorValue) /
Math.Log(2))) + 1;
}
int disSimilarBitPosition = maxBitCount - bitCountXorValue;
// calculate result by formula
int result = bitCount1 + bitCount2 - 2 * disSimilarBitPosition;
return result;
}
// Driver code
public static void Main(String []args)
{
int n1 = 12, n2 = 5;
Console.WriteLine(minDistance(n1, n2));
}
}
/* This code contributed by PrinciRaj1992 */
PHP
$bitCount2)
{
$n2 = $n2 * pow(2, $bitDiff);
}
else
{
$n1 = $n1 * pow(2, $bitDiff);
}
$xorValue = $n1 ^ $n2;
$bitCountXorValue = floor(log($xorValue, 2)) + 1;
$disSimilarBitPosition = $maxBitCount -
$bitCountXorValue;
// calculate result by formula
$result = $bitCount1 + $bitCount2 - 2 *
$disSimilarBitPosition;
return $result;
}
// Driver Code
$n1 = 12;
$n2 = 5;
echo minDistance($n1, $n2);
// This code is contributed by akt_mit
?>
Javascript
输出:
5