0
0
DSA Javascriptprogramming~10 mins

Tree Traversal Inorder Left Root Right in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Tree Traversal Inorder Left Root Right
Start at root node
Traverse left subtree
Visit current node (root)
Traverse right subtree
End traversal
Inorder traversal visits the left subtree first, then the root node, and finally the right subtree.
Execution Sample
DSA Javascript
function inorder(node) {
  if (node === null) return;
  inorder(node.left);
  console.log(node.value);
  inorder(node.right);
}
This code prints the values of a binary tree in inorder sequence: left child, root, then right child.
Execution Table
StepOperationNode VisitedPointer ChangesTree State (Visual)
1Start at root10current = 10 10 / \ 5 15
2Traverse left subtree5current = 5 10 / \ 5 15
3Traverse left subtreenullcurrent = null 10 / \ 5 15
4Visit node5print 5 10 / \ 5 15
5Traverse right subtreenullcurrent = null 10 / \ 5 15
6Return to root10current = 10 10 / \ 5 15
7Visit node10print 10 10 / \ 5 15
8Traverse right subtree15current = 15 10 / \ 5 15
9Traverse left subtreenullcurrent = null 10 / \ 5 15
10Visit node15print 15 10 / \ 5 15
11Traverse right subtreenullcurrent = null 10 / \ 5 15
12Traversal completenullcurrent = null 10 / \ 5 15
💡 Traversal ends when all nodes have been visited and current pointer is null.
Variable Tracker
VariableStartAfter Step 2After Step 6After Step 8Final
current10 (root)5 (left child)10 (root)15 (right child)null (end)
printed_values[][][5][5, 10][5, 10, 15]
Key Moments - 3 Insights
Why do we visit the left subtree before the root node?
In the execution_table at steps 2 and 4, we see the traversal goes left first to node 5 before printing it, ensuring left nodes are processed before the root.
What happens when the current node is null?
At steps 3, 5, 9, and 11, current becomes null, which means there is no child node to visit, so the function returns and backtracks to the previous node.
Why do we print the node value after traversing the left subtree?
Step 4 shows printing after left traversal, ensuring the left subtree is fully processed before visiting the root, following the inorder rule.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the printed value at step 7?
A10
B5
C15
Dnull
💡 Hint
Check the 'Operation' and 'Node Visited' columns at step 7 in the execution_table.
At which step does the traversal visit the right child of the root node?
AStep 2
BStep 8
CStep 6
DStep 10
💡 Hint
Look for 'Traverse right subtree' operation and 'Node Visited' being 15 in the execution_table.
If the left child of the root was null, how would the printed values change?
A[5, 10, 15]
B[15, 10, 5]
C[10, 15]
D[10]
💡 Hint
Refer to variable_tracker and consider what happens when left subtree traversal is skipped.
Concept Snapshot
Inorder traversal visits nodes in Left → Root → Right order.
Use recursion: traverse left subtree, visit node, traverse right subtree.
Print node value after left subtree is done.
Useful for binary search trees to get sorted order.
Stops when all nodes are visited (null reached).
Full Transcript
Inorder traversal means visiting the left child first, then the root node, and finally the right child. The code uses recursion to go as far left as possible, then prints the node's value, and then goes right. The execution table shows each step: starting at root 10, going left to 5, printing 5, returning to 10, printing 10, then going right to 15 and printing 15. When a node is null, the function returns and backtracks. This traversal order is useful because it prints nodes in sorted order for binary search trees.