Binary Tree Inorder Traversal

Easy#94in-order

You get the root of a binary tree. Return its values in in-order: the left subtree, then the node, then the right subtree. Our tree: 6 with children 2 and 9; 2 has children 7 and 4; 9 has only a right child, 3 — the answer is [7, 2, 4, 6, 9, 3].

input
root: a binary tree, written as on LeetCode: its values level by level, left to right, null for a missing child.
returns
Its values in inorder: for each node, its left subtree first, then the node, then its right subtree. An empty tree gives [].
constraints
  • 0 ≤ the number of nodes ≤ 100
  • −100 ≤ Node.val ≤ 100
our 12 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
function inorderTraversal(root) {

}
Ctrl ↵
format
reset

saved in this browser

12 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more tree traversals problems

Pattern “Tree traversals” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.