Simple Python Solution


  • 0
    A

    from collections import deque
    class Solution(object):

    def preorderTraversal(self, root):
    
        if not root:
            return []
        q = deque()
        q.append(root)
        items = []
        while len(q) != 0:
            node = q.popleft()
            if node.right:
                q.appendleft(node.right)
            if node.left:
                q.appendleft(node.left)
            items.append(node.val)
        
        return items

Log in to reply
 

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