Inorder Successor in BST
great solution, thx!
@mach7 So the reason why your teacher's code is so long is because it works for ANY binary tree, not just binary search tree.
Also, here my version of the code that'll work for any binary tree. It's O(N), so not great.
Such nice solution, thx.
This solution is not complete. It will return some node in the tree if p doesn't exist in the tree. Should have returned NULL or error code.
That's ok. At least here at LeetCode, BSTs can't contain duplicates. See the definition here or here.
I method 1, p is not used???
No one has replied
love the solution; also love the comments
Thanks, Stefan. O(logn) is for the normal cases when tree is balanced. You are right that it is O(n) at worst case
Update: Added some optimization to the original code - skip the left subtree if p.val >= root.val during the traversal.
I can not figure out why in the findNext() function, when root==p, we need to return null?
Disabled Categories are greyed out
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.