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.
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.
- Sort the path from root to a given node in a Binary Tree
- Maximum XOR with given value in the path from root to given node in the tree
- Print path from root to all nodes in a Complete 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
- Find if there is a pair in root to a leaf path with sum equals to root's data
- Find the maximum sum leaf to root path in a Binary Tree
- Boundary Root to Leaf Path traversal of a Binary Tree
- Maximize count of set bits in a root to leaf path in a binary tree
- Find all root to leaf path sum of a Binary Tree
- Count nodes having highest value in the path from root to itself in a Binary Tree
- Count nodes having smallest value in the path from root to itself in a Binary Tree
- Find distance from root to given node in a binary tree
- Print the path common to the two paths from the root to the two given nodes
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Given a binary tree, print all root-to-leaf paths
- Find the path from root to the given nodes of a tree for multiple queries
- Sum of nodes on the longest path from root to leaf node
- Find node U containing all nodes from a set V at atmost distance 1 from the path from root to U
- Print common nodes on path from root (or common ancestors)