Check if two nodes are on same path in a tree | Set 2

Given two nodes of a binary tree v1 and v2, the task is to check if two nodes are on the same path in a tree.

Example:

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 Approach: Refer to Check if two nodes are on same path in a tree for the DFS approach.

LCA Approach: The idea is to use Lowest Common Ancestor. Find the LCA of the given vertices v1 and v2. If the LCA is equal to any of the given two vertices, print Yes. Otherwise, print No.

Below is the implementation of above approach:



C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check if two nodes
// are on same path in a tree without
// using any extra space
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

Yes

Time Complexity: O(N)
Auxiliary Space: O(1)

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Rajput-Ji