Skip to content

Latest commit

 

History

History

285-InorderSuccessorinBST

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Inorder Successor in BST

Problem can be found in [here](https://leetcode.com/problems/inorder-successor-in-bst/!

# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
def inorderSuccessor(root: TreeNode, p: TreeNode) -> Optional[TreeNode]:
    successor = None
    while root:
        if p.val >= root.val:
            root = root.right
        else:
            successor = root
            root = root.left

    return successor

Time Complexity: O(n), Space Complexity: O(1)