Given a BST and a number. The task is to check whether the given number is equal to the sum of all the node from root leaf across any of the root to leaf paths in the given Binary Search Tree.
Approach: The idea is to traverse from root to all leaves in top-down fashion maintaining a path array to store current root to leaf path. While traversing, store data of all nodes of current path in the array path. Whenever a leaf node is reached, calculate the sum of all of the nodes on the current path using the array path and check if it is equal to the given sum.
Below is the implementation of above approach:
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.