AAlgoLoopSpaced repetition for LeetCode
EASYTree / RecursionLeetCode ↗

Binary Tree Inorder Traversal

The key idea

Inorder means left, node, right: for every node, fully traverse its left subtree before recording the node's own value, then traverse its right subtree. Applying that rule recursively at every node lays the values out in the inorder sequence — and for a binary search tree that sequence is sorted.

Problem

Given the root of a binary tree, return the inorder traversal of its nodes' values.

Inorder traversal visits the left subtree, then the current node, then the right subtree. For each node, you record its value only after every node in its left subtree has been visited.

Follow-up: The recursive solution is trivial. Could you do it iteratively instead?

Constraints

Examples

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

Complexity

Time: O(n) Space: O(n)

See the full solution

410310
Step-by-step visualization
Start free →

More Tree / Recursion problems