Amazon.com Interview Question
1,580 Interview Reviews |
Back to all Amazon.com Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Computer Science Intern at Amazon.com:
You are given a Binary Search Tree of ints with duplicate entries. You are guaranteed that if an element has a duplicate entry, it is in the right subtree of that element. Write a function to count the total number of duplicates. (note, if a tree contains the same element two times, the count of duplicates is 1, not 2)
| Tags: | technical, brain teaser, algorithm, difficult, recursive algorithm See more , See less 8 |
See more for this Amazon.com Computer Science Intern Interview
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (2)
0 of 1 people found this helpful
int DupCount( Node *root )
{
int count = 0;
DoDupCount(root, count);
Return count;
}
void DoDupCount(Node *root, int &count)
{
if (root == NULL )
return;
// traversal , go left first
DoDupCount(root->left, count);
Bool flag = false;
// traversal, go right until met different node
While(root->right != NULL && root->right->data == root->data )
{
flag = true;
root= root->right;
}
if (flag)
count++;
DoDupCount(root->right, count);
}
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up



0 of 0 people found this helpful
by Interview Candidate: