• RSS
  • Facebook
  • Twitter

Knowledge is Power.

  • Who you are ?

    Working on machines without understanding them ? Then you should be here..

  • Where you are ?

    Geographical location should not become a barrier to Share our knowledge.

  • What do you do ?

    Puzzles and Interview question are intended to be discussed here.

    Sunday, February 21, 2010

    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);
    }
    }

    0 comments:

    Post a Comment