What is DOM Traversal: Depth-First Search?
Walk the DOM tree by following one branch as deep as possible before backtracking.
Type a tool name, format or task
Walk the DOM tree by following one branch as deep as possible before backtracking. Interactive step-by-step animation with JavaScript code, complexity and front-end use cases.
Loading animation...
Walk the DOM tree by following one branch as deep as possible before backtracking. Visit the current element. Recursively visit each child in order, going all the way down first. Backtrack up when a branch ends. Time complexity: O(n). Space complexity: O(h) for tree height h. querySelector, finding nested elements, copying or serializing a subtree and walking component trees.
Walk the DOM tree by following one branch as deep as possible before backtracking.
Time: O(n). Space: O(h) for tree height h.
querySelector, finding nested elements, copying or serializing a subtree and walking component trees.
No. This converter runs in your browser for ordinary use, so the input is not sent to a backend by this tool.
Keep working without searching again.
Depth-first search goes as deep as possible down one branch before trying the next, looking for <button>. It is how querySelector walks the DOM.
function dfs(node, target, visit) {visit(node);if (node.tag === target) return node;for (const child of node.children) {const found = dfs(child, target, visit);if (found) return found;}return null;}
Tip: use the arrow keys to step and the space bar to play or pause.