Menu

Sign in to track your progress and unlock all features.

Theme style

Log in
01. Implement preorder traversalE02. Implement inorder traversalE03. Implement postorder traversalE

Problem No 2

Implement inorder traversal

Easy

8 minute session

Summary

Implement inorder (left-root-right) traversal for a binary tree, returning the visited node values as a list.

Problem statement

Given the root of a binary tree, implement a function inorder(root) that returns a list of node values visited in inorder (left, root, right). Use the provided Node class. Handle an empty tree by returning an empty list.

Task

Write inorder(root) to traverse left subtree, visit root, then right subtree, returning list of values.

Examples

Simple tree

Input

inorder(Node(2, Node(1), Node(3)))

Output

[1, 2, 3]

Explanation

Visit left child 1, then root 2, then right child 3.

Input format

A single argument: root (Node or None).

Output format

A list of integers representing node values in inorder order.

Constraints

Tree size up to 10^4. Use recursion or iteration. Do not print; return the list.

Samples

Sample input 0

inorder(Node(1))

Sample output 0

[1]

Explanation 0

Single node tree returns its value in a list.

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

03:44 PM

Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.