GeeksforGeeks » Interview Questions

Trees

(4 posts)

Tags:

  1. Agniswar
    Member
    Posted 9 months ago #

    Given a binary tree , return the root of the largest BST in it if present else return null.

  2. Anonymous

    Posted 9 months ago #

    Node bst=NULL;
    bool checkIfBST(Node node)
    {

    if(node == NULL)
    return true;
    if(checkIfBST(node->left) && checkIfBST(node->right))
    {
    bool isBST=(node->left != NULL ? node->value > node->left->value : true) && (node->right != NULL ? node->value < node->right->value : true);
    if(isBST)
    {
    bst=node;
    return isBST;
    }
    else
    return false;
    }
    }

  3. Agniswar
    Member
    Posted 9 months ago #

    This algorithm will only return whether there exist a BST in the given tree or not..but the question is to find the largest possible BST in the tree..so how does your solution ensure that it is the largest one ..please tell me if i am wrong.

  4. Mani
    guest
    Posted 9 months ago #

    It actually returns the largest possible BST.
    It checks from the leaf nodes to root whether the tree is BST or not. If its BST then the "bst" pointer gets updated .


Reply

You must log in to post.

RSS feed for this topic