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:
7 3 1 4
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print path between any two nodes in a Binary Tree | Set 2
- XOR of path between any two nodes in a Binary Tree
- Shortest path between two nodes in array like representation of binary tree
- Maximum weighted edge in path between two nodes in an N-ary tree using binary lifting
- Minimum difference between any two weighted nodes in Sum Tree of the given Tree
- Print path from root to all nodes in a Complete Binary Tree
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Print the path common to the two paths from the root to the two given nodes
- Print nodes between two given level numbers of a binary tree
- Print all nodes between two given levels in Binary Tree
- Queries to check if the path between two nodes in a tree is a palindrome
- Sum of all odd nodes in the path connecting two given nodes
- Count of 1's in any path in a Binary Tree
- Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
- Count nodes having smallest value in the path from root to itself in a Binary Tree
- Count nodes having highest value in the path from root to itself in a Binary Tree
- Find the maximum path sum between two leaves of a binary tree
- Minimum sum path between two leaves of a binary tree
- Check if two nodes are on same path in a tree | Set 2
- Least Common Ancestor of any number of 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 email@example.com. 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.