Given a Binary Tree of distinct nodes and a pair of nodes. The task is to find and print the path between the two given nodes in the binary tree.

For Example, in the above binary tree the path between the nodes 7 and 4 is 7 -> 3 -> 1 -> 4.
The idea is to find paths form root nodes to the two nodes and store them in two separate vectors or arrays say path1 and path2.
Now, there arises two different cases:
- If the two nodes are in different subtrees of root nodes. That is one in the left subtree and the other in the right subtree. In this case it is clear that root node will lie in between the path from node1 to node2. So, print path1 in reverse order and then path 2.
- If the nodes are in the same subtree. That is either in the left subtree or in the right subtree. In this case you need to observe that path from root to the two nodes will have an intersection point before which the path is common for the two nodes from the root node. Find that intersection point and print nodes from that point in a similar fashion of the above case.
Below is the implementation of the above approach:
C++
// C++ program to print path between any // two nodes in a Binary Tree #include <bits/stdc++.h> using namespace std; // structure of a node of binary tree struct Node { int data; Node *left, *right; }; /* Helper function that allocates a new node with the given data and NULL left and right pointers. */struct Node* getNode(int data) { struct Node* newNode = new Node; newNode->data = data; newNode->left = newNode->right = NULL; return newNode; } // Function to check if there is a path from root // to the given node. It also populates // 'arr' with the given path bool getPath(Node* root, vector<int>& arr, int x) { // if root is NULL // there is no path if (!root) return false; // push the node's value in 'arr' arr.push_back(root->data); // if it is the required node // return true if (root->data == x) return true; // else check whether the required node lies // in the left subtree or right subtree of // the current node if (getPath(root->left, arr, x) || getPath(root->right, arr, x)) return true; // required node does not lie either in the // left or right subtree of the current node // Thus, remove current node's value from // 'arr'and then return false arr.pop_back(); return false; } // Function to print the path between // any two nodes in a binary tree void printPathBetweenNodes(Node* root, int n1, int n2) { // vector to store the path of // first node n1 from root vector<int> path1; // vector to store the path of // second node n2 from root vector<int> path2; getPath(root, path1, n1); getPath(root, path2, n2); int intersection = -1; // Get intersection point int i = 0, j = 0; while (i != path1.size() || j != path2.size()) { // Keep moving forward until no intersection // is found if (i == j && path1[i] == path2[j]) { i++; j++; } else { intersection = j - 1; break; } } // Print the required path for (int i = path1.size() - 1; i > intersection; i--) cout << path1[i] << " "; for (int i = intersection; i < path2.size(); i++) cout << path2[i] << " "; } // Driver program int main() { // binary tree formation struct Node* root = getNode(0); root->left = getNode(1); root->left->left = getNode(3); root->left->left->left = getNode(7); root->left->right = getNode(4); root->left->right->left = getNode(8); root->left->right->right = getNode(9); root->right = getNode(2); root->right->left = getNode(5); root->right->right = getNode(6); int node1 = 7; int node2 = 4; printPathBetweenNodes(root, node1, node2); return 0; } |
Java
// Java program to print path between any // two nodes in a Binary Tree import java.util.*; class Solution { // structure of a node of binary tree static class Node { int data; Node left, right; } /* Helper function that allocates a new node with the given data and null left and right pointers. */ static Node getNode(int data) { Node newNode = new Node(); newNode.data = data; newNode.left = newNode.right = null; return newNode; } // Function to check if there is a path from root // to the given node. It also populates // 'arr' with the given path static boolean getPath(Node root, Vector<Integer> arr, int x) { // if root is null // there is no path if (root==null) return false; // push the node's value in 'arr' arr.add(root.data); // if it is the required node // return true if (root.data == x) return true; // else check whether the required node lies // in the left subtree or right subtree of // the current node if (getPath(root.left, arr, x) || getPath(root.right, arr, x)) return true; // required node does not lie either in the // left or right subtree of the current node // Thus, remove current node's value from // 'arr'and then return false arr.remove(arr.size()-1); return false; } // Function to print the path between // any two nodes in a binary tree static void printPathBetweenNodes(Node root, int n1, int n2) { // vector to store the path of // first node n1 from root Vector<Integer> path1= new Vector<Integer>(); // vector to store the path of // second node n2 from root Vector<Integer> path2=new Vector<Integer>(); getPath(root, path1, n1); getPath(root, path2, n2); int intersection = -1; // Get intersection point int i = 0, j = 0; while (i != path1.size() || j != path2.size()) { // Keep moving forward until no intersection // is found if (i == j && path1.get(i) == path2.get(i)) { i++; j++; } else { intersection = j - 1; break; } } // Print the required path for ( i = path1.size() - 1; i > intersection; i--) System.out.print( path1.get(i) + " "); for ( i = intersection; i < path2.size(); i++) System.out.print( path2.get(i) + " "); } // Driver program public static void main(String[] args) { // binary tree formation Node root = getNode(0); root.left = getNode(1); root.left.left = getNode(3); root.left.left.left = getNode(7); root.left.right = getNode(4); root.left.right.left = getNode(8); root.left.right.right = getNode(9); root.right = getNode(2); root.right.left = getNode(5); root.right.right = getNode(6); int node1 = 7; int node2 = 4; printPathBetweenNodes(root, node1, node2); } } //contributed by Arnab Kundu |
7 3 1 4
Recommended Posts:
- XOR of path between any two nodes in a Binary Tree
- Shortest path between two nodes in array like representation of binary tree
- Print path from root to a given node in a binary tree
- Print Levels of all nodes in a Binary Tree
- Print nodes in the Top View of Binary Tree | Set 3
- Print all full nodes in a Binary Tree
- Print all even nodes of Binary Search Tree
- Print all nodes between two given levels in Binary Tree
- Print Nodes in Top View of Binary Tree
- Print all nodes in a binary tree having K leaves
- Print nodes in top view of Binary Tree | Set 2
- Print the nodes of binary tree as they become the leaf node
- Print nodes between two given level numbers of a binary tree
- Print all leaf nodes of a Binary Tree from left to right
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
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 : andrew1234



