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.
- Root to leaf path sum equal to a given number
- Shortest root to leaf path sum equal to a given number
- Implementing a BST where every node stores the maximum number of nodes in the path till any leaf
- Print the longest path from root to leaf in a Binary tree
- Number of elements smaller than root using preorder traversal of a BST
- Remove nodes on root to leaf paths of length < K
- Count of root to leaf paths whose permutation is palindrome in a Binary Tree
- Find maximum GCD value from root to leaf in a Binary tree
- Find k-th smallest element in BST (Order Statistics in BST)
- Two nodes of a BST are swapped, correct the BST
- K'th Largest Element in BST when modification to BST is not allowed
- Convert a normal BST to Balanced BST
- Two nodes of a BST are swapped, correct the BST | Set-2
- Reverse a path in BST using queue
- Largest number in BST which is less than or equal to N
- Largest number less than or equal to N in BST (Iterative Approach)
- Smallest number in BST which is greater than or equal to N
- Smallest number in BST which is greater than or equal to N ( Iterative Approach)
- Find a pair with given sum in a Balanced BST
- Find a pair with given sum in BST
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.