python recursion, no helper function needed


  • 0
    class Solution(object):
        def preorderTraversal(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            if not root: return []
            out = []
            out.append(root.val)
            out.extend(self.preorderTraversal(root.left))
            out.extend(self.preorderTraversal(root.right))
            return out
    

Log in to reply
 

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