0
0
DSA Goprogramming~10 mins

Tree Traversal Inorder Left Root Right in DSA Go - 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
Inorder traversal visits nodes by first going left, then the root, then right.
Execution Sample
DSA Go
func inorder(node *Node) {
  if node == nil {
    return
  }
  inorder(node.Left)
  fmt.Print(node.Value, " ")
  inorder(node.Right)
}
This code prints tree nodes in inorder: left subtree, root, then right subtree.
Execution Table
StepOperationNode VisitedLeft/RightTree State (Inorder Visited)
1Start at root10root[]
2Traverse left subtree5left[]
3Traverse left subtree3left[]
4Traverse left subtreenilleft[]
5Visit node3root[3]
6Traverse right subtreenilright[3]
7Return to parent5root[3]
8Visit node5root[3,5]
9Traverse right subtree7right[3,5]
10Traverse left subtreenilleft[3,5]
11Visit node7root[3,5,7]
12Traverse right subtreenilright[3,5,7]
13Return to parent10root[3,5,7]
14Visit node10root[3,5,7,10]
15Traverse right subtree15right[3,5,7,10]
16Traverse left subtree13left[3,5,7,10]
17Traverse left subtreenilleft[3,5,7,10]
18Visit node13root[3,5,7,10,13]
19Traverse right subtreenilright[3,5,7,10,13]
20Return to parent15root[3,5,7,10,13]
21Visit node15root[3,5,7,10,13,15]
22Traverse right subtree18right[3,5,7,10,13,15]
23Traverse left subtreenilleft[3,5,7,10,13,15]
24Visit node18root[3,5,7,10,13,15,18]
25Traverse right subtreenilright[3,5,7,10,13,15,18]
26Endnilnone[3,5,7,10,13,15,18]
💡 Traversal ends after visiting all nodes in left-root-right order.
Variable Tracker
VariableStartAfter Step 5After Step 8After Step 11After Step 14After Step 18After Step 21After Step 24Final
Current Node1035710131518nil
Inorder Visited[][3][3,5][3,5,7][3,5,7,10][3,5,7,10,13][3,5,7,10,13,15][3,5,7,10,13,15,18][3,5,7,10,13,15,18]
Key Moments - 3 Insights
Why do we visit the node only after traversing its left subtree?
Because inorder means Left-Root-Right, so we must first go all the way left before visiting the root node, as shown in steps 2-5 in the execution_table.
What happens when the node is nil?
The function returns immediately without action, stopping traversal down that path, as seen in steps 4, 6, 10, 12, 17, 19, 23, 25.
Why do we return to the parent node after finishing left and right subtrees?
Because inorder traversal visits nodes in left-root-right order, after finishing left and right children, we return to the parent to continue traversal, shown in steps 7 and 13.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the inorder visited list after step 14?
A[3,5,7]
B[3,5,7,10]
C[3,5]
D[3,5,7,10,13]
💡 Hint
Check the 'Tree State (Inorder Visited)' column at step 14 in execution_table.
At which step does the traversal visit node 13?
AStep 18
BStep 16
CStep 20
DStep 21
💡 Hint
Look for 'Visit node' operation with node 13 in execution_table.
If the left child of node 5 was nil, how would the inorder visited list change after step 8?
A[5,7]
B[3,5]
C[5]
D[3,7]
💡 Hint
Without left child of 5, node 3 is never visited, so after visiting 5, list is just [5].
Concept Snapshot
Inorder Tree Traversal:
- Visit left subtree first
- Then visit root node
- Then visit right subtree
- Recursive approach: call inorder(left), print root, call inorder(right)
- Result: nodes visited in ascending order for BST
Full Transcript
Inorder traversal means visiting the left subtree, then the root node, then the right subtree. The code recursively calls itself on the left child until it reaches nil, then visits the node, then calls itself on the right child. The execution table shows each step visiting nodes 3, 5, 7, 10, 13, 15, and 18 in order. Variables track the current node and the list of visited nodes. Key moments clarify why nodes are visited after left subtree and what happens when nodes are nil. The quiz tests understanding of the visited order and traversal steps.