My Python generator inorder traversal


  • 0
    R
    class Solution(object):
        def inorderSuccessor(self, root, p):
            """
            :type root: TreeNode
            :type p: TreeNode
            :rtype: TreeNode
            """
            foundP = False
            for i in self.inorder(root):
                if i == p:
                    foundP = True
                    continue
                if foundP:
                    return i
            return None
        def inorder(self, node):
            if node.left:
                for ele in self.inorder(node.left):
                    yield ele
            yield node
            if node.right:
                for ele in self.inorder(node.right):
                    yield ele

Log in to reply
 

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