views:

133

answers:

1

How can we find level order successor of a node in bst if parent pointer is given(with out using Queue ) ?

A: 

In the basic case, it's the right sibling of node. Otherwise you need to wraparound to the next level, or return No successor.

Go up to the next parent with a right child, and traverse it left back down to level of node. If you're able to trace back to root with no right children, go down the left side to level + 1. If you reach an empty child ptr, return no successor.

If it's not a full BST, you may need to do a little more work - repeating until you find a node at the desired level.

Stephen
It's fine.But I'm still thinking to do it in O(n) time and O(1) space
dsiap
@dslap : this solution is O(n) time, O(1) space.
Stephen
213 4 5 6 7Here your algorithm has to return 7 for LOS(6) .Will it do ?There are so much cases to be considered like this
dsiap