Python short solution


  • 0
    Z
    class Solution(object):
        def inorderSuccessor(self, root, p):
            """
            :type root: TreeNode
            :type p: TreeNode
            :rtype: TreeNode
            """
            
            
            # this can solve the problem,
            # but not a good method
            #if root
            def flatten(root):
                if not root:
                    return []
                else:
                    return flatten(root.left)+[root]+flatten(root.right)
            nodeList = flatten(root)
            for i in range(len(nodeList)-1):
                if p==nodeList[i]:
                    return nodeList[i+1]
            else:
                return None

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.