Skip to content

Instantly share code, notes, and snippets.

@vamsitallapudi
Created December 18, 2020 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vamsitallapudi/3654dcef136bdfb541b8f951c7574f5c to your computer and use it in GitHub Desktop.
Save vamsitallapudi/3654dcef136bdfb541b8f951c7574f5c to your computer and use it in GitHub Desktop.
class Solution:
def preorderTraversal(self, root: TreeNode) -> List[int]:
# to return an empty array if root is empty
if not root:
return []
# appending the root value in array and recursively going into left and right subtrees
return [root.val] + self.preorderTraversal(root.left) + self.preorderTraversal(root.right)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment