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);
}
}
0 comments:
Post a Comment