binary-tree
depth-first-search
recursion
stack

Given the root of a binary tree, return its in-order traversal as an array of node values. In-order means you visit the left subtree first, then the current node, then the right subtree.

If the tree is empty, return an empty array.

Input / output

  • Input: root: TreeNode (the JSON test fixture is a LeetCode-style level-order array, e.g. [1,null,2,3], using null for missing children)
  • Output: int[]

Constraints

  • 0 <= number of nodes <= 100
  • -100 <= Node.val <= 100

Follow-up

The recursive solution is natural here, but can you also solve it iteratively using an explicit stack?

Examples

Example 1

Input: root = [1,null,2,3]
Output: [1,3,2]

Example 2

Input: root = []
Output: []

Example 3

Input: root = [1]
Output: [1]