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 |
Examples :
Input:
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.
Input:
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:
C++
// C++ code to count special two-digit // numbers in BST#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left;Node*right;Node(intx){data=x;left=right=nullptr;}};// Function to check if a number is a special // two-digit numberboolisSpecial(intnum){if(num<10||num>99)returnfalse;intfirstDigit=num/10;intsecondDigit=num%10;// Check if the sum of digits and product of // digits equals the numberreturn(firstDigit+secondDigit+firstDigit*secondDigit==num);}// Recursive function to count special two-digit // numbers in BSTintcountSpecialNumbers(Node*root){if(root==nullptr)return0;// Count special numbers in left and right// subtrees recursivelyintleftCount=countSpecialNumbers(root->left);intrightCount=countSpecialNumbers(root->right);// Check if current node's data is a special// two-digit numberintcurrentCount=isSpecial(root->data)?1:0;returnleftCount+rightCount+currentCount;}intmain(){// Binary Search Tree representation:// 19// / \ // 1 99// /// 57// /// 22Node*root=newNode(19);root->left=newNode(1);root->right=newNode(99);root->right->left=newNode(57);root->right->left->left=newNode(22);cout<<countSpecialNumbers(root)<<endl;return0;}
C
// C code to count special two-digit// numbers in BST#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*left;structNode*right;};// Function to check if a number is a special// two-digit numberintisSpecial(intnum){if(num<10||num>99)return0;intfirstDigit=num/10;intsecondDigit=num%10;// Check if the sum of digits and product of// digits equals the numberreturn(firstDigit+secondDigit+firstDigit*secondDigit==num);}// Recursive function to count special // two-digit numbers in BSTintcountSpecialNumbers(structNode*root){if(root==NULL)return0;// Count special numbers in left and right// subtrees recursivelyintleftCount=countSpecialNumbers(root->left);intrightCount=countSpecialNumbers(root->right);// Check if current node's data is a // special two-digit numberintcurrentCount=isSpecial(root->data)?1:0;returnleftCount+rightCount+currentCount;}structNode*createNode(intdata){structNode*node=(structNode*)malloc(sizeof(structNode));node->data=data;node->left=NULL;node->right=NULL;returnnode;}intmain(){// Binary Search Tree representation:// 19// / \ // 1 99// /// 57// /// 22structNode*root=createNode(19);root->left=createNode(1);root->right=createNode(99);root->right->left=createNode(57);root->right->left->left=createNode(22);printf("%d\n",countSpecialNumbers(root));return0;}
Java
// Java code to count special two-digit // numbers in BSTimportjava.util.*;classNode{intdata;Nodeleft,right;Node(intx){data=x;left=right=null;}}classGfG{// Function to check if a number is a // special two-digit numberstaticbooleanisSpecial(intnum){if(num<10||num>99)returnfalse;intfirstDigit=num/10;intsecondDigit=num%10;// Check if the sum of digits and product // of digits equals the numberreturn(firstDigit+secondDigit+firstDigit*secondDigit==num);}// Recursive function to count special // two-digit numbers in BSTstaticintcountSpecialNumbers(Noderoot){if(root==null)return0;// Count special numbers in left and right // subtrees recursivelyintleftCount=countSpecialNumbers(root.left);intrightCount=countSpecialNumbers(root.right);// Check if current node's data is a// special two-digit numberintcurrentCount=isSpecial(root.data)?1:0;returnleftCount+rightCount+currentCount;}publicstaticvoidmain(String[]args){// Binary Search Tree representation:// 19// / \// 1 99// /// 57// /// 22Noderoot=newNode(19);root.left=newNode(1);root.right=newNode(99);root.right.left=newNode(57);root.right.left.left=newNode(22);System.out.println(countSpecialNumbers(root));}}
Python
# Python code to count special # two-digit numbers in BSTclassNode:def__init__(self,data):self.data=dataself.left=Noneself.right=None# Function to check if a number is a special# two-digit numberdefis_special(num):ifnum<10ornum>99:returnFalsefirst_digit=num//10second_digit=num%10# Check if the sum of digits and product of # digits equals the numberreturnfirst_digit+second_digit \
+first_digit*second_digit==num# Recursive function to count special # two-digit numbers in BSTdefcount_special_numbers(root):ifrootisNone:return0# Count special numbers in left and right # subtrees recursivelyleft_count=count_special_numbers(root.left)right_count=count_special_numbers(root.right)# Check if current node's data is a # special two-digit numbercurrent_count=1ifis_special(root.data)else0returnleft_count+right_count+current_countif__name__=="__main__":# Binary Search Tree representation:# 19# / \# 1 99# /# 57# /# 22root=Node(19)root.left=Node(1)root.right=Node(99)root.right.left=Node(57)root.right.left.left=Node(22)print(count_special_numbers(root))
C#
// C# code to count special two-digit// numbers in BSTusingSystem;classNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=right=null;}}classGfG{// Function to check if a number is a // special two-digit numberstaticboolIsSpecial(intnum){if(num<10||num>99)returnfalse;intfirst_digit=num/10;intsecond_digit=num%10;// Check if the sum of digits and product of // digits equals the numberreturn(first_digit+second_digit+first_digit*second_digit==num);}// Recursive function to count special // two-digit numbers in BSTstaticintCountSpecialNumbers(Noderoot){if(root==null)return0;// Count special numbers in left and // right subtrees recursivelyintleft_count=CountSpecialNumbers(root.left);intright_count=CountSpecialNumbers(root.right);// Check if current node's data is a // special two-digit numberintcurrent_count=IsSpecial(root.data)?1:0;returnleft_count+right_count+current_count;}staticvoidMain(string[]args){// Binary Search Tree representation:// 19// / \// 1 99// /// 57// /// 22Noderoot=newNode(19);root.left=newNode(1);root.right=newNode(99);root.right.left=newNode(57);root.right.left.left=newNode(22);Console.WriteLine(CountSpecialNumbers(root));}}
JavaScript
// JavaScript code to count special // two-digit numbers in BSTclassNode{constructor(data){this.data=data;this.left=null;this.right=null;}}// Function to check if a number is a special // two-digit numberfunctionisSpecial(num){if(num<10||num>99){returnfalse;}constfirstDigit=Math.floor(num/10);constsecondDigit=num%10;// Check if the sum of digits and product of // digits equals the numberreturnfirstDigit+secondDigit+firstDigit*secondDigit===num;}// Recursive function to count special // two-digit numbers in BSTfunctioncountSpecialNumbers(root){if(root===null){return0;}// Count special numbers in left and right // subtrees recursivelyconstleftCount=countSpecialNumbers(root.left);constrightCount=countSpecialNumbers(root.right);// Check if current node's data is a special // two-digit numberconstcurrentCount=isSpecial(root.data)?1:0;returnleftCount+rightCount+currentCount;}// Binary Search Tree representation:// 19// / \// 1 99// /// 57// /// 22constroot=newNode(19);root.left=newNode(1);root.right=newNode(99);root.right.left=newNode(57);root.right.left.left=newNode(22);console.log(countSpecialNumbers(root));
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.