Accepted python recursive simple solution

  • -1
    # Definition for a  binary tree node
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    class Solution:
        # @param root, a tree node
        # @return a list of integers
        def preorderTraversal(self, root):
            if not root:
                return []
            return [root.val] + self.preorderTraversal(root.left) + self.preorderTraversal(root.right)

Log in to reply

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