Inorder Successor in BST

只要在迭代时不停的更新每一个值比大的结点到suc变量里面就行了

class Solution(object):
    def inorderSuccessor(self, root, p):
        """
        :type root: TreeNode
        :type p: TreeNode
        :rtype: TreeNode
        """
        suc = None
        while root:
            if root.val <= p.val:
                root = root.right
            else:
                suc = root
                root = root.left

        return suc

results matching ""

    No results matching ""