Root To Node Path in Binary Tree

DFS with backtracking path visualizer

Read Here
Target
Step0/32
PathNot Found
Back To Trees List

Python Code

3
def dfs(node):
5 if node is None:
6 return False
7
8 path.append(node.data)
9
10 if node.data == target:
11 return True
12
13 if dfs(node.left):
14 return True
15
16 if dfs(node.right):
17 return True
18
19 path.pop()
20 return False
Current Line (4): DFS Function Entry

Tree Structure

1234567
Operation:ENTER

Traversal Progress

Current Node

-

Phase

Enter Function

Current Path

Path evolves here while DFS explores...
Step 1: ENTER

Recursion Stack

Stack is empty. Click Next to begin!

Step Explanation

Ready to Start

Click "Next Step" to search path for target 7. DFS will push nodes and backtrack on dead ends.

  • > Start from dfs(root).
  • > Watch call stack, highlighted line, and path array together.
Unvisited
Left
Current
Right
Done