Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Phone Interview
// Looks like I was logged in. So posting again.
fun(Node n) {
if (n == null) return 0;
int lval = fun(n.left), rval = fun(n.right);
n.value = lval - rval;
return lval + rval;
}
Assume Node has (int) data as member:
public int LeftMinusRight(Node root)
{
return Sum(root.Left) - Sum(root.Right);
}
private int Sum(Node node)
{
return Sum(node.Left) + Sum(node.Right);
}
Please let me know if anything is missing.
- Aashish July 05, 2012