TreeNode(val, left=None, right=None) is in the starter. Keep it.
Every tree recursion is one of three orders, depending on when you handle the node relative to its two subtrees. Write all three, each returning the list of values visited:
preorder(root): node, then left subtree, then right subtree.inorder(root): left subtree, then node, then right subtree. (For a binary search tree this is sorted order.)postorder(root): left subtree, then right subtree, then node.
An empty tree (None) gives [].
# 1
# / \
# 2 3
# / \
# 4 5
preorder(root) # [1, 2, 4, 5, 3]
inorder(root) # [4, 2, 5, 1, 3]
postorder(root) # [4, 5, 2, 3, 1]
Trees have at most 500 nodes, so plain recursion is fine here. Values may repeat or be negative.
Show hint
the three functions share one recursive skeleton (if node is None: return, recurse left, recurse right); only the line that records node.val moves.