Thoughts: Utilize the binary search property of BST: during the traversal: if a node is about to traverse to its left. Then record the node as successor before doing so.
Code: T: O(h) -> O(logn)
Python
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def inorderSuccessor(self, root, p):
"""
:type root: TreeNode
:type p: TreeNode
:rtype: TreeNode
"""
successor = None
while root:
if p.val < root.val:
successor = root
root = root.left
else:
root = root.right
return successor