Qn:
Write a function to count the number of leaf nodes in a binary search tree.
Solution: count = leaf nodes in left subtree + leaf nodes in right subtree
If a node is having left link as NULL and right link as NULL, then it
is a leaf node. So if this condition is true, the call should return 1.
If the node is NULL, the call should return 0.
- if node is NULL
- return 0
- if node->left==NULL and node->right==NULL
- return 1
- else
- return count(node->left)+count(node->right)
Comments
Post a Comment