Binary Tree Inorder Traversal
Easy
Topics
Given the root of a binary tree, return its inorder traversal (left, node, right) as a list of values.
Example 1
visited first
The small number above each node is its position in the output: left subtree, node, right subtree, giving
[1, 3, 2]. For a BST this is the one order that comes out sorted. The iterative version swaps the call stack for an explicit one.Tree given in level-order (breadth-first);
null marks a missing child.Input: root = [1,null,2,3] Output: [1,3,2]
Example 2
empty tree
Input: root = [] Output: []
Constraints
- 0 <= number of nodes <= 100
- -100 <= Node.val <= 100
Editorial — intuition, approach, complexity and pitfalls
Intuition
Inorder means: fully visit the left subtree, then the node, then the right subtree. Recursion expresses this directly; an explicit stack gives the iterative version.
Approach
- Recurse left.
- Append the current node’s value.
- Recurse right.
Dry Run
1
/ \
2 3
/ \
4 5
at 1: go left
at 2: go left
at 4: go left -> null
at 4: VISIT res [4]
at 4: go right -> null
at 2: VISIT res [4, 2]
at 2: go right
at 5: go left -> null
at 5: VISIT res [4, 2, 5]
at 5: go right -> null
at 1: VISIT res [4, 2, 5, 1]
at 1: go right
at 3: go left -> null
at 3: VISIT res [4, 2, 5, 1, 3]
at 3: go right -> null
answer 4 2 5 1 3
The node is appended at the moment control comes back from its left child and
before it descends right. That is the whole content of the word "inorder", and it
is why the root lands in the MIDDLE of the output rather than at either end.
Move res.append above dfs(node.left) and these same three lines emit 1 2 4 5 3,
which is preorder.
Worth noticing in the trace: 5 nodes but 11 calls. Every absent child still costs
a call that returns immediately, so an n-node tree makes n + 1 null calls.Complexity
- Time — O(n): each node visited once.
- Space — O(h): recursion (or explicit) stack up to height h.
Common Pitfalls
- The visit sits between the two recursive calls. Moving
res.appendabovedfs(node.left)gives preorder, not inorder -- the same three lines in a different order return1 2 4 5 3where inorder gives4 2 5 1 3. - Inorder on a BST is the sorted order. That is the property most problems are really reaching for, and it is worth saying so rather than treating the traversal as an end in itself.
Full Solution — all languages
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def inorderTraversal(self, root):
res = []
def dfs(node):
if not node:
return
dfs(node.left)
res.append(node.val)
dfs(node.right)
dfs(root)
return resimport java.util.*;
class TreeNode {
int val;
TreeNode left, right;
TreeNode(int v) {
val = v;
}
}
class Solution {
public int[] inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Deque<TreeNode> st = new ArrayDeque<>();
TreeNode cur = root;
while (cur != null || !st.isEmpty()) {
while (cur != null) {
st.push(cur);
cur = cur.left;
}
cur = st.pop();
res.add(cur.val);
cur = cur.right;
}
int[] a = new int[res.size()];
for (int i = 0; i < a.length; i++) a[i] = res.get(i);
return a;
}
}#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
int val;
TreeNode *left, *right;
TreeNode(int v): val(v), left(nullptr), right(nullptr) {
}
};
class Solution {
public: vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
function<void(TreeNode*)> dfs = [&](TreeNode* node) {
if (!node) return;
dfs(node->left);
res.push_back(node->val);
dfs(node->right);
};
dfs(root);
return res;
}
};/**
* Tree node shape: { val, left, right }
* @param {TreeNode} root
* @return {number[]}
*/ var inorderTraversal = function(root) {
const res = [];
const dfs = node => {
if (!node) return;
dfs(node.left);
res.push(node.val);
dfs(node.right);
};
dfs(root);
return res;
};