📌  相关文章
📜  检查两个节点是否在树中的同一路径上。套装2

📅  最后修改于: 2021-06-26 13:47:27             🧑  作者: Mango

给定二叉树v1v2的两个节点,任务是检查两个节点是否在树中的同一路径上。
例子:

Input:  v1 = 1, v2 = 5
       1
    /  |  \
   2   3   4
  /    |    \
 5     6     7
 
Output: Yes
Explanation:
Both nodes 1 and 5
lie in the path 1 -> 2 -> 5.

Input: v1 = 2, v2 = 6
       1
    /  |  \
   2   3   4
  /    |    \
 5     6     7

Output: NO

DFS方法:有关DFS方法,请参阅检查树中两个节点是否在同一路径上。
LCA方法:这个想法是使用最低共同祖先。找到给定顶点v1v2的LCA。如果LCA等于给定两个顶点中的任何一个,则打印“是” 。否则,打印No。

下面是上述方法的实现:

C++
// C++ program to check if two nodes
// are on same path in a tree without
// using any extra space
#include 
using namespace std;
  
// Function to filter
// the return Values
int filter(int x, int y, int z)
{
    if (x != -1 && y != -1) {
        return z;
    }
    return x == -1 ? y : x;
}
  
// Utility function to check if nodes
// are on same path or not
int samePathUtil(int mtrx[][7], int vrtx,
                 int v1, int v2, int i)
{
    int ans = -1;
  
    // Condition to check
    // if any vertex
    // is equal to given two
    // vertex or not
    if (i == v1 || i == v2)
        return i;
  
    for (int j = 0; j < vrtx; j++) {
  
        // Check if the current
        // position has 1
        if (mtrx[i][j] == 1) {
            // Recursive call
            ans
                = filter(ans,
                         samePathUtil(mtrx,
                                      vrtx,
                                      v1,
                                      v2,
                                      j),
                         i);
        }
    }
  
    // Return LCA
    return ans;
}
  
// Function to check if nodes
// lies on same path or not
bool isVertexAtSamePath(int mtrx[][7],
                        int vrtx, int v1,
                        int v2, int i)
{
    int lca = samePathUtil(mtrx,
                           vrtx, v1 - 1,
                           v2 - 1, i);
  
    if (lca == v1 - 1 || lca == v2 - 1)
        return true;
  
    return false;
}
  
// Driver Program
int main()
{
    int vrtx = 7, edge = 6;
    int mtrx[7][7] = {
        { 0, 1, 1, 1, 0, 0, 0 },
        { 0, 0, 0, 0, 1, 0, 0 },
        { 0, 0, 0, 0, 0, 1, 0 },
        { 0, 0, 0, 0, 0, 0, 1 },
        { 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0 }
  
    };
  
    int v1 = 1, v2 = 5;
  
    if (isVertexAtSamePath(mtrx,
                           vrtx, v1,
                           v2, 0))
        cout << "Yes";
  
    else
        cout << "No";
  
    return 0;
}


Java
// Java program to check if two nodes
// are on same path in a tree without
// using any extra space
class GFG{
  
// Function to filter
// the return Values
static int filter(int x, int y, int z)
{
    if (x != -1 && y != -1)
    {
        return z;
    }
    return x == -1 ? y : x;
}
  
// Utility function to check if nodes
// are on same path or not
static int samePathUtil(int mtrx[][], int vrtx,
                        int v1, int v2, int i)
{
    int ans = -1;
  
    // Condition to check
    // if any vertex
    // is equal to given two
    // vertex or not
    if (i == v1 || i == v2)
        return i;
  
    for(int j = 0; j < vrtx; j++)
    {
          
        // Check if the current
        // position has 1
        if (mtrx[i][j] == 1)
        {
              
            // Recursive call
            ans = filter(ans, samePathUtil(
                         mtrx, vrtx, v1,
                         v2, j), i);
        }
    }
  
    // Return LCA
    return ans;
}
  
// Function to check if nodes
// lies on same path or not
static boolean isVertexAtSamePath(int mtrx[][],
                                  int vrtx, int v1,
                                  int v2, int i)
{
    int lca = samePathUtil(mtrx, vrtx, v1 - 1,
                                       v2 - 1, i);
                                         
    if (lca == v1 - 1 || lca == v2 - 1)
        return true;
  
    return false;
}
  
// Driver code
public static void main(String[] args)
{
    int vrtx = 7;
    int mtrx[][] = { { 0, 1, 1, 1, 0, 0, 0 },
                     { 0, 0, 0, 0, 1, 0, 0 },
                     { 0, 0, 0, 0, 0, 1, 0 },
                     { 0, 0, 0, 0, 0, 0, 1 },
                     { 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0 } };
  
    int v1 = 1, v2 = 5;
  
    if (isVertexAtSamePath(mtrx, vrtx,
                           v1, v2, 0))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 program to check if two nodes
# are on same path in a tree without
# using any extra space
  
# Function to filter
# the return Values
def filter(x, y, z):
      
    if (x != -1 and y != -1):
        return z
      
    return y if x == -1 else x
  
# Utility function to check if nodes
# are on same path or not
def samePathUtil(mtrx, vrtx, v1, v2, i):
      
    ans = -1
  
    # Condition to check
    # if any vertex
    # is equal to given two
    # vertex or not
    if (i == v1 or i == v2):
        return i
      
    for j in range(0, vrtx):
          
        # Check if the current
        # position has 1
        if (mtrx[i][j] == 1):
              
            # Recursive call
            ans = filter(ans, 
                         samePathUtil(mtrx, vrtx, 
                                   v1, v2, j), i)
          
    # Return LCA
    return ans
  
# Function to check if nodes
# lies on same path or not
def isVertexAtSamePath(mtrx, vrtx, v1, v2, i):
      
    lca = samePathUtil(mtrx, vrtx, v1 - 1,
                                   v2 - 1, i)
  
    if (lca == v1 - 1 or lca == v2 - 1):
        return True
  
    return False
  
# Driver code
vrtx = 7
edge = 6
  
mtrx = [ [ 0, 1, 1, 1, 0, 0, 0 ] ,
         [ 0, 0, 0, 0, 1, 0, 0 ],
         [ 0, 0, 0, 0, 0, 1, 0 ],
         [ 0, 0, 0, 0, 0, 0, 1 ],
         [ 0, 0, 0, 0, 0, 0, 0 ],
         [ 0, 0, 0, 0, 0, 0, 0 ],
         [ 0, 0, 0, 0, 0, 0, 0 ] ]
  
v1 = 1
v2 = 5
  
if (isVertexAtSamePath(mtrx, vrtx, v1, v2, 0)):
    print("Yes")
else:
    print("No")
  
# This code is contributed by sanjoy_62


C#
// C# program to check if two nodes
// are on same path in a tree without
// using any extra space
using System;
class GFG{
  
// Function to filter
// the return Values
static int filter(int x, int y, int z)
{
    if (x != -1 && y != -1)
    {
        return z;
    }
    return x == -1 ? y : x;
}
  
// Utility function to check if nodes
// are on same path or not
static int samePathUtil(int [,]mtrx, int vrtx,
                        int v1, int v2, int i)
{
    int ans = -1;
  
    // Condition to check
    // if any vertex
    // is equal to given two
    // vertex or not
    if (i == v1 || i == v2)
        return i;
  
    for(int j = 0; j < vrtx; j++)
    {
          
        // Check if the current
        // position has 1
        if (mtrx[i,j] == 1)
        {
              
            // Recursive call
            ans = filter(ans, samePathUtil(
                         mtrx, vrtx, v1,
                         v2, j), i);
        }
    }
  
    // Return LCA
    return ans;
}
  
// Function to check if nodes
// lies on same path or not
static bool isVertexAtSamePath(int [,]mtrx,
                               int vrtx, int v1,
                               int v2, int i)
{
    int lca = samePathUtil(mtrx, vrtx, v1 - 1,
                                       v2 - 1, i);
                                         
    if (lca == v1 - 1 || lca == v2 - 1)
        return true;
  
    return false;
}
  
// Driver code
public static void Main(String[] args)
{
    int vrtx = 7;
    int [,]mtrx = { { 0, 1, 1, 1, 0, 0, 0 },
                    { 0, 0, 0, 0, 1, 0, 0 },
                    { 0, 0, 0, 0, 0, 1, 0 },
                    { 0, 0, 0, 0, 0, 0, 1 },
                    { 0, 0, 0, 0, 0, 0, 0 },
                    { 0, 0, 0, 0, 0, 0, 0 },
                    { 0, 0, 0, 0, 0, 0, 0 } };
  
    int v1 = 1, v2 = 5;
  
    if (isVertexAtSamePath(mtrx, vrtx,
                           v1, v2, 0))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
  
// This code is contributed by sapnasingh4991


输出:
Yes

时间复杂度: O(N)
辅助空间: O(1)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。