Finding width of a BST is not very difficult of you think about it. We just need to find the maximum of the left subtree and the left subtree width and add the root element to it.
Solution:
private int maxDepth (Node node)
{
if(node == null ) return 0;
else
{
int ldepth = maxDepth(node.left);
int rdepth = maxDepth(node.right);
//use the larger of both + 1
return(java.lang.Math.max(ldepth, rdepth) + 1);
}
}
Solution:
private int maxDepth (Node node)
{
if(node == null ) return 0;
else
{
int ldepth = maxDepth(node.left);
int rdepth = maxDepth(node.right);
//use the larger of both + 1
return(java.lang.Math.max(ldepth, rdepth) + 1);
}
}



Working on machines without understanding them ? Then you should be here..
Geographical location should not become a barrier to Share our knowledge.
Puzzles and Interview question are intended to be discussed here.
0 comments:
Post a Comment