Amazon Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Written Test
I am not sure if I catch the question, e.g. (in fact, it is a binary, not a BST)
5
4 8
6 9
so , the output is
5,
6,
9
because the distance between all of them and start node 8 is 1, right?
#include <iostream>
using namespace std;
struct TreeNode {
TreeNode(): _left_child(0),_right_child(0),_data(0) {}
TreeNode* _left_child;
TreeNode* _right_child;
int _data;
};
// helper Method to print the node.
// Store it into a data structure if required
void Print(TreeNode* node)
{
if(node) {
cout<<node->_data<<endl;
}
}
void FindKDistanceNode(TreeNode *root, int k)
{
if (!root) return;
if (k == 0) {
Print(root);
} else {
FindKDistanceNode(root->_left_child, k-1);
FindKDistanceNode(root->_right_child, k-1);
}
}
int SearchNode (TreeNode *root, TreeNode *n, int k)
{
if (!root) return -1;
if (root == n) {
// node is found
if(k==0) {
// If distance is zero then print the node itself.
Print(root);
return -1;
} else {
FindKDistanceNode(root->_right_child, k-1);
FindKDistanceNode(root->_left_child, k-1);
return 1; //return 1 which the distance from the node
// searched in the last call. i.e. the parent node.
}
}
int left_count = SearchNode(root->_left_child, n, k);
int right_count = SearchNode(root->_right_child, n, k);
if(left_count>0) {
if((k-left_count) == 0) {
Print(root);
return -1;
} else {
FindKDistanceNode(root->_right_child, k - left_count-1);
return ++left_count; // Going on step in heirarchy. So increase the distance by one.
}
}
if(right_count>0) {
if((k-right_count) == 0) {
Print(root);
return -1;
} else {
FindKDistanceNode(root->_left_child, k - right_count-1);
return ++right_count; // Going on step in heirarchy. So increase the distance by one.
}
}
return -1;
}
/*
* Sample Tree
*
* 12
* 13 10
* 8 2 7 4
*5 15 17 9 22 23 30 1
*
*
* For Child2 i.e. 13 nodes at distance
* K=0 -> 13
* k=1 -> 8, 2, 12
* k=2 -> 5, 15, 17 ,9, 10
* k=3 -> 7, 4
*/
int main(int argc, char** argv)
{
TreeNode* root;
TreeNode child1;
child1._data = 12;
TreeNode child2;
child2._data = 13;
TreeNode child3;
child3._data = 10;
TreeNode child4;
child4._data = 8;
TreeNode child5;
child5._data = 2;
TreeNode child6;
child6._data = 7;
TreeNode child7;
child7._data = 4;
TreeNode child8;
child8._data = 5;
TreeNode child9;
child9._data = 15;
TreeNode child10;
child10._data = 17;
TreeNode child11;
child11._data = 9;
TreeNode child12;
child12._data = 22;
TreeNode child13;
child13._data = 23;
TreeNode child14;
child14._data = 30;
TreeNode child15;
child15._data = 1;
root = &child1;
child1._left_child = &child2;
child1._right_child = &child3;
child2._left_child = &child4;
child2._right_child = &child5;
child3._left_child = &child6;
child3._right_child = &child7;
child4._left_child = &child8;
child4._right_child = &child9;
child5._left_child = &child10;
child5._right_child = &child11;
child6._left_child = &child12;
child6._right_child = &child13;
child7._left_child = &child14;
child7._right_child = &child15;
SearchNode(root, &child2, 2);
return 0;
}
void PrintKDistanceNodes(Node* root, int distance, int k)
{
if (root == NULL || distance == 0) return;
if (distance == k)
{
printf("%d\n", root->data);
}
--distance;
PrintDistanceNodes(root->left, distance, k);
PrintDistanceNodes(root->right, distance, k);
}
You have considered only the nodes which are descendants of the start node,but we also have to consider the predecessors also (forward or backward).
Haha.. thats correct..
However Answer is Simple.. they are asking the modifed BFS which is easy .
here it goes.
void BFS(Struct Node *node,int Distance)
{
Set S;
Queue Q;
Q.enqueue("node");
while (Q.isempty()) [
Struct Node *n=Q.dequeue();
if(!S.contains(n)) {
S.add(n); Distance++;
cout<<" Distance of the Node from Root is" <<Distance<<endl;
for (int i=0;i<=2/** This can be N **/;i++)
BFS(n->outgoing[i],Distance);
}
}
//Call it with BFS (root,0);
Do BFS from start node and find the nodes which has distance K, print them.
The other solution:
Distance (Node *node, dist, k, Node *parent, Node *child) {
if (node == NULL)
return;
if (dist == k) {
print (node->data);
return;
}
// Passing parent and child to avoid the same node to be visited again.
if (node->left != child)
Distance (node->left, dist+1, k, node, NULL);
if (node->parent != parent)
Distance (node->parent, dist+1, k, NULL, node);
if (node->right != child)
Distance (node->right, dist+1, k, node, NULL);
}
main () {
Distance (Node *node, 0,k, NULL, NULL);
}
@devesh
Do you always follow syntax? There is a thing called logic. Btw you can modify the function internally.
Assumptions : There is no duplicate data in BST
The following code will print all the k distance node from a given node in unsorted order.
- Bidhan Pal June 25, 2012