/144. Binary Tree Preorder Traversal

144. Binary Tree Preorder Traversal

Easy
Stack75.3% acceptance

Given a binary tree represented by its root node, return a list containing the values of the nodes in preorder traversal order (visit root, then left subtree, then right subtree).

Example 1

Input: TreeNode(10, TreeNode(5), TreeNode(15))

Output: [10, 5, 15]

Explanation: Root 10, left 5, right 15.

Example 2

Input: TreeNode(7, TreeNode(3, None, TreeNode(4)), TreeNode(9))

Output: [7, 3, 4, 9]

Explanation: Root 7, left subtree 3->4, right 9.

Example 3

Input: None

Output: []

Explanation: Empty tree.

Constraints

  • 0 <= number of nodes <= 100
  • -100 <= node value <= 100
Python (current runtime)

Case 1

Input: TreeNode(2, TreeNode(1), TreeNode(3))

Expected: [2, 1, 3]

Case 2

Input: TreeNode(8, None, TreeNode(10, TreeNode(9), None))

Expected: [8, 10, 9]

Case 3

Input: TreeNode(0)

Expected: [0]

Case 4

Input: TreeNode(-5, TreeNode(-10), TreeNode(-3))

Expected: [-5, -10, -3]