Microsoft Interview Question
Web Developerssimpe...
void callF(Node* root, void(*fun)(void *) )
{
if(root ==NULL) return;
if(root->left) callF(root->left, fun);
if(root->right)callF(root->right, fun);
if(!root->left && !root->right)
(*fun)();
}
And here is the Java version : It uses java's reflection mechanism
public void traverseAndCallFunction(AvlNode root, String functionName)
{
if(root == null) return;
else
{
traverseAndCallFunction(root.left, functionName);
traverseAndCallFunction(root.right, functionName);
if(root.left == null && root.right== null)
{
try{
this.getClass().getMethod(functionName).invoke(this, (Object[]) null);
}catch(Exception e)
{
e.printStackTrace();
}
}
}
}
public void dummyFunction()
{
System.out.println("Dummy Function called using reflection technique of Java");
}
???
- dal December 04, 2007I wasn't ready for that one...