parameters: (self, root, p)
return_statement: return successor
docstring: :type root: TreeNode
          :type p: TreeNode
          :rtype: TreeNode
docstring_summary: :type root: TreeNode
          :type p: TreeNode
          :rtype: TreeNode
docstring_tokens: : type root : TreeNode : type p : TreeNode : rtype : TreeNode
function: def inorderSuccessor(self, root, p):
          """
          :type root: TreeNode
          :type p: TreeNode
          :rtype: TreeNode
          """
          # If it has right subtree.
          if p and p.right:
              p = p.right
              while p.left:
                  p = p.left
              return p
  
          # Search from root.
          successor = None
          while root and root != p:
              if root.val > p.val:
                  successor = root
                  root = root.left
              else:
                  root = root.right
  
          return successor
function_tokens: def inorderSuccessor ( self , root , p ) : # If it has right subtree. if p and p . right : p = p . right while p . left : p = p . left return p # Search from root. successor = None while root and root != p : if root . val > p . val : successor = root root = root . left else : root = root . right return successor