3

The best algorithm to verify if a binary tree is a BST is given as follows

IsValidBST(root,-infinity,infinity);

bool IsValidBST(BinaryNode node, int MIN, int MAX) 
{
    if(node == null)
        return true;
    if(node.element > MIN 
        && node.element < MAX
        && IsValidBST(node.left,MIN,node.element)
        && IsValidBST(node.right,node.element,MAX))
        return true;
    else 
        return false;
}

The space complexity of this logic is apparently O(logN) which I'm assuming is the cost of recursion. How was the value arrived at?

10
  • 2
    There is no additional data used other than the method variables and the return value, so indeed, all memory is "cost of recursion". The total cost would hence be linearly proportional to the depth of the tree. In a balanced binary search tree, the depth is O(log n), so indeed, the space complexity would be O(log n) too. However, in general a BST is not necessarily balanced, it could even be a chain of length n, if the root is the minimum, its right child is the second smallest element, and so on. In that case the space complexity of this recursion is O(n). Commented Feb 4, 2014 at 7:46
  • 2
    @Heuster Good comment, but it should really be the answer. Commented Feb 4, 2014 at 7:47
  • How is the question related to c or c++? Does it matter in any way here what language you use? Commented Feb 4, 2014 at 7:47
  • @user694733 I guess you're right :-) Commented Feb 4, 2014 at 7:49
  • 1
    Note: it can be done in O(1) if each node has a pointer to its parent, because you can then iterate without maintaining a stack. So it fairly depends on how the binary tree is represented. Commented Feb 4, 2014 at 8:25

1 Answer 1

12

My comment upgraded to answer:

There is no additional data used other than the method variables and the return value, so indeed, all memory is "cost of recursion". The total cost would hence be linearly proportional to the depth of the tree.

In a balanced binary search tree, the depth is O(log n), so indeed, the space complexity would be O(log n) too. However, in general a BST is not necessarily balanced, it could even be a chain of length n, if the root is the minimum, its right child is the second smallest element, and so on. In that case the space complexity of this recursion is O(n).

Sign up to request clarification or add additional context in comments.

9 Comments

but aren't you adding one entry to the recursive stack for each node? isn't that O(n) regardless of how the BST is constructed?
The stack is cleaned up when the recursive function reaches a leaf node. So if the tree is balanced, the stack will never be deeper than log n.
Thank you, but we have elements on both sides of the BST, so why it's O(log n) please?
@avra, see Mats' comment above yours. Elements are popped from the stack on the way "up", so the size of the stack doesn't exceed the depth of the tree.
@VincentvanderWeele. Thank you very much! So last question is, why we should have complexity of O(log n) when we do check each left and right subtree at each node? If we have n nodes, then we should have O(nlog n) compelxity for checking validity of BST, please correct if I am wrong? (This is for bruteforce solution not for the above).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.