Given a Binary Search Tree, the task is to count the number of nodes which are having special two-digit numbers. Note: Special two-digit numbers are those numbers that satisfy the condition that the sum of their digits added to the product of their digits equals the original number. Prerequisite:Special Two Digit Number | Binary Search Tree |
Output: 2 Explanation: The two special two-digit numbers in the input are 19 and 99. These numbers satisfy the condition of special two-digit numbers. Hence, the output is 2.
Output: 0 Explanation: There are no special two-digit number in the given input.
Approach:
The idea is to iterate through each node of tree recursively with a variable count, and check each node's data for a special two-digit number. If it is then increment the variable count. In the end, return count.
Below is the implementation of the above approach:
Output
2
Time Complexity: O(n), thatn is the number of nodes in Tree. Auxiliary Space: O(h),where h is the height of the tree and this extra space is used due to the recursion call stack.