Bloomberg LP Interview Question
Financial Software DevelopersCountry: United States
Interview Type: Phone Interview
Given a tree with a single root node, this function returns depth 1. When by definition, the depth in this case should be 0.
recursivedepth(valueh,root)
{
valueh++;
if(valueh >height)
{
height=valueh;
}
if(root.lchild !=null)
{
recursivedepth(valueh,root.lchild);
}
if(root.rchild !=null)
{
recursivedepth(valueh,root.rchild);
}
}
I donno whether this is the optimal solution for this problem are can come up with some other
static int maxDepth = 0;
public static void getMaxDepth(Node node, int curDepth) {
if(node == null) return;
curDepth++;
if(maxDepth<curDepth) maxDepth++;
getMaxDepth(node.getLeft(), curDepth);
getMaxDepth(node.getRight(), curDepth);
}
- sharath February 06, 2013