National Instruments Interview Question
Software Engineer / DevelopersSolution 1: Using Marker byte.
Solution 2: Using Property check, BST condition will fail. left<parent<right
Solution 3: like in Linked list, maintain 2 pointers,
if(ptr1==ptr2) loop detected
for a node call
ptr1 = node->left
ptr2 = node->left->left and also node->left->right
AND
ptr1 = node->right
ptr2 = node->right>left and also node->right>right
Use recursion its very simple...to calculate sum and use static variable to calculate the number of nodes available and simple divide them.
The base case for the recursion will be:
{if (head of node is == null) return 0;}
else keep callin the same function with the argument as its child:
else
return (sum(left.child) + sum(right.child) + nodes' value);
and
static variable count = 0; and in each else count++;
1. do a inorder traversal, maintain a static counter for no of elemnts, maintain a static sum variable..your job is done..
- Vishal Goswami. March 04, 2007