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.
Input: N1 = 7, N2 = 4
Output: 7 3 1 4
Approach: An approach to solve this problem has been discussed in this article. In this article, an even optimised recursive approach will be discussed.
In this recursive approach, below are the steps:
- Find the first value recursively, once found add the value to the stack.
- Now every node that is visited whether in backtracking or forward tracking, add the values to the stack but if the node was added in the forward tracking then remove it in the backtracking and continue this until the second value is found or all nodes are visited.
For example: Consider the path between 7 and 9 is to be found in the above tree. We traverse the tree as DFS, once we find the value 7, we add it to the stack. Traversing path 0 -> 1 -> 3 -> 7.
Now while backtracking, add 3 and 1 to the stack. So as of now, the stack has [7, 3, 1], child 1 has a right child so we first add it to the stack. Now, the stack contains [7, 3, 1, 4]. Visit the left child of 4, add it to the stack. The stack contains [7, 3, 1, 4, 8] now. Since there is no further node we would go back to the previous node and since 8 was already added to the stack so remove it. Now the node 4 has a right child and we add it to the stack since this is the second value we were looking for there won’t be any further recursive calls. Finally, the stack contains [7, 3, 1, 4, 9].
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
- 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
- Print the path common to the two paths from the root to the two given nodes
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- 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
- Check if two nodes are on same path in a tree | Set 2
- Sum of all odd nodes in the path connecting two given nodes
- Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
- Count of 1's in any path 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
- Maximum absolute difference between any two level sum in a Binary Tree
- Least Common Ancestor of any number of nodes in Binary Tree
- Print nodes in top view of Binary Tree | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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