Easy Preorder Traversal Python, DFS


  • 0
    W
    class Solution(object):
        def flatten(self, root):
            """
            :type root: TreeNode
            :rtype: void Do not return anything, modify root in-place instead.
            """
            if not root:
                return None
            s,start=[],root
            s.append(root)
            start=TreeNode(None)
            a=start
            while s:
                node=s.pop(-1)
                start.right=node
                start.left=None
                start=node
                if node.right:
                    s.append(node.right)
                if node.left:
                    s.append(node.left)
            root=a.right

Log in to reply
 

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