Binary Tree Preorder Traversal
- python
- binary trees
- preorder traversal
- dfs
Problem URL:Binary Tree Preorder Traversal
My Solution
Python
Recursive Approach
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def preorderTraversal(root: Optional[TreeNode]) -> List[int]:
if not root:
return []
return [root.val, *preorderTraversal(root.left), *preorderTraversal(root.right)]
Let's Connect
Twitter •GitHub •LinkedIn