number of nodes in its child-trees in addition to the key.
Example:
(50,4)
/ \
(30,4) (70,2)
/ \ / \
(20,2) (40,0) (60,0) (80,0)
/ \
(10,0) (25,0)
Given, 10, 40, the required value is 3, since we have 20, 25, 30
Answer:
int FindCount(TreeNode* n, int min_val, int max_val)
{
if (n)
{
if ( n->val is between min_val and max_val )
return n->child_count - 2;
else if ( n->val < min_val )
return FindCount(n->right, min_val, max_val);
else
return FindCount(n->left, min_val, max_val);
}
return 0;
}
No comments:
Post a Comment