/94. Binary Tree Inorder Traversal

94. Binary Tree Inorder Traversal

Easy
Stack79.8% acceptance

Given the root node of a binary tree, return a list containing the values of the nodes in inorder traversal (left subtree, root, right subtree). The input tree is represented as a TreeNode object. If the tree is empty, return an empty list.

Example 1

Input: TreeNode(5, TreeNode(3), TreeNode(7))

Output: [3, 5, 7]

Explanation: Inorder traversal: left (3), root (5), right (7).

Example 2

Input: TreeNode(10, None, TreeNode(12, TreeNode(11), None))

Output: [10, 11, 12]

Explanation: Inorder traversal: root (10), left of right (11), right (12).

Example 3

Input: None

Output: []

Explanation: Empty tree returns empty list.

Example 4

Input: TreeNode(1, TreeNode(0), None)

Output: [0, 1]

Explanation: Inorder traversal: left (0), root (1).

Constraints

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

Case 1

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

Expected: [1, 2, 3, 4]

Case 2

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

Expected: [8, 9]

Case 3

Input: TreeNode(6, TreeNode(5, None, TreeNode(5)), None)

Expected: [5, 5, 6]

Case 4

Input: TreeNode(0)

Expected: [0]