18. Sum of leaf nodes in BST
The problem can be found at the following link: Question Link
My Approach
If the root is null, it returns 0.
If the root has no left and right children (i.e., it's a leaf node), it returns the value of that node.
Otherwise, it recursively calls itself on the left and right subtrees and returns the sum of the results.
Time and Auxiliary Space Complexity
Time Complexity :
O(N)
, whereN
is the number of nodes in the Binary Search Tree.Auxiliary Space Complexity :
O(H)
, whereH
is the height of the Binary Search Tree.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.
Last updated