[
bst
]
BinarySearch 0440 Inorder Successor
Problem statement
https://binarysearch.com/problems/Inorder-Successor/
Solution
Equal to Leetcode 0285. Inorder Successor in BST
Complexity
Time complexity for unbalanced trees can be O(n)
, for balanced it is just O(log n)
, space complexity is O(1)
.
Code
class Solution:
def solve(self, root, t):
successor = None
node = root
while node:
if node.val <= t:
node = node.right
else:
successor = node.val
node = node.left
return successor