Given a binary tree with distinct nodes(no two nodes have the same have data values). The problem is to print the path from root to a given node x. If node x is not present then print “No Path”.
Input : 1 / \ 2 3 / \ / \ 4 5 6 7 x = 5 Output : 1->2->5
Approach: Create a recursive function that traverses the different path in the binary tree to find the required node x. If node x is present then it returns true and accumulates the path nodes in some array arr. Else it returns false.
Following are the cases during the traversal:
- If root = NULL, return false.
- push the root’s data into arr.
- if root’s data = x, return true.
- if node x is present in root’s left or right subtree, return true.
- Else remove root’s data value from arr and return false.
This recursive function can be accessed from other function to check whether node x is present or not and if it is present, then the path nodes can be accessed from arr. You can define arr globally or pass its reference to the recursive function.
Time complexity: O(n) in worst case, where n is the number of nodes in the binary tree.
This article is contributed by Ayush Jauhari. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Sort the path from root to a given node in a Binary Tree
- Print the longest path from root to leaf in a Binary tree
- Print the first shortest root to leaf path in a Binary Tree
- Print path from root to all nodes in a Complete Binary Tree
- Maximum XOR with given value in the path from root to given node in the tree
- Boundary Root to Leaf Path traversal of a Binary Tree
- Find the maximum sum leaf to root path in a Binary Tree
- Find distance from root to given node in a binary tree
- Print all the paths from root, with a specified sum in Binary tree
- Given a binary tree, print all root-to-leaf paths
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Print path between any two nodes in a Binary Tree | Set 2
- Print path between any two nodes in a Binary Tree
- Print all Coprime path of a Binary Tree
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Print all paths of the Binary Tree with maximum element in each path greater than or equal to K
- Print cousins of a given node in Binary Tree
- Print Ancestors of a given node in Binary Tree
- Print the nodes of binary tree as they become the leaf node
- Print the number of set bits in each node of a Binary Tree