Binary Tree Preorder Traversal Visualizer & Step-by-Step Algorithm Solution

Visit binary tree nodes in Root -> Left -> Right order using DFS recursion and call stack unwinding.

Category: trees | Difficulty: Easy

Tags: DFS, Binary Tree, Recursion, Preorder

Binary Tree Preorder Traversal

4
2
1
3
7
6
9
100%
state
result[]
Initialization
1/38
Explanation

Start preorder traversal on binary tree with root: Node(4).

Source Code
1function preorder(node, result = []) {
2 if (!node) return result;
3 result.push(node.val); // Process Root
4 preorder(node.left, result); // Recurse Left
5 preorder(node.right, result); // Recurse Right
6 return result;
7}