Python solution


  • 0
    X
    class Solution:
        # @param {TreeNode} root
        # @return {integer[]}
        def preorderTraversal(self, root):
            Solution.ans = []
            if not root: return Solution.ans
            self.preorder(root,Solution.ans)
            return Solution.ans
    
    
        def preorder(self,root,ans):
            if root:
                ans.append(root.val)
                self.preorder(root.left,ans)
                self.preorder(root.right,ans)

Log in to reply
 

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