Traverse binary trees in preorder and inorder
Quick answer
A binary tree has at most two child links per node. Traversal visits every reachable node in a defined order; preorder visits root first, inorder visits root between its children, and postorder visits root last.
Working examples
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
const tree={v:2,left:{v:5,left:null,right:null},right:{v:1,left:null,right:null}};const pre=[],inside=[];function visit(n){if(!n)return;pre.push(n.v);visit(n.left);inside.push(n.v);visit(n.right);}visit(tree);console.log(JSON.stringify(pre),JSON.stringify(inside));Expected console output:
[2,5,1] [5,2,1]The root is emitted before or between child visits. The arbitrary tree is not a BST.
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
const t={v:"A",left:{v:"B",left:null,right:null},right:{v:"C",left:null,right:null}};const out=[];function post(n){if(!n)return;post(n.left);post(n.right);out.push(n.v);}post(t);console.log(out.join(""));Expected console output:
BCAPostorder processes both children before the root.
How it works and cost model
For an acyclic tree with no shared nodes, recurse on left and right subtrees, using null as the base case. For preorder emit root before left and right; inorder emit left, root, right; postorder emit after both. Each node is visited once: O(n) time and O(h) active recursion frames for height h measured in levels, excluding output. A skewed tree can have height n; use an explicit stack for deep structures. Inorder yields sorted keys only if the tree satisfies a binary-search-tree ordering.
Common mistakes
Do not assume inorder sorts an arbitrary binary tree. Graphs with cycles or shared nodes need a different visitation policy.
Practice
Create a root with two children and a left grandchild. Record preorder, inorder and postorder, then compare the visit order.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Traverse binary trees in preorder and inorder — Knowledge Check
Reason about the algorithm or data structure, its invariants, trade-offs and deterministic outputs.
Where is a node emitted during an inorder traversal?
When does inorder traversal produce ascending numeric keys?
What determines the maximum number of pending recursive tree visits?
What is the exact console output in the Binary-tree traversal example 1?
const tree={v:2,left:{v:5,left:null,right:null},right:{v:1,left:null,right:null}};const pre=[],inside=[];function visit(n){if(!n)return;pre.push(n.v);visit(n.left);inside.push(n.v);visit(n.right);}visit(tree);console.log(JSON.stringify(pre),JSON.stringify(inside));What is the exact console output in the Binary-tree traversal example 2?
const t={v:"A",left:{v:"B",left:null,right:null},right:{v:"C",left:null,right:null}};const out=[];function post(n){if(!n)return;post(n.left);post(n.right);out.push(n.v);}post(t);console.log(out.join(""));