0
0
DSA C++programming~10 mins

Tree Traversal Inorder Left Root Right in DSA C++ - 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
Return to parent or end
Inorder traversal visits the left subtree first, then the root node, and finally the right subtree, recursively.
Execution Sample
DSA C++
struct Node {
  int val;
  Node* left;
  Node* right;
};

void inorder(Node* root) {
  if (!root) return;
  inorder(root->left);
  std::cout << root->val << " ";
  inorder(root->right);
}
This code recursively visits left child, prints root value, then visits right child.
Execution Table
StepOperationNode VisitedLeft/RightTree State
1Start at root1root1 / \ 2 3
2Traverse left subtree2left of 11 / \ 2 3
3Traverse left subtree4left of 21 / \ 2 3 / 4
4Visit node4left leaf4 visited
5Return to parent2back to 24 visited
6Visit node2left child4 -> 2 visited
7Traverse right subtree5right of 24 -> 2 visited
8Visit node5right leaf4 -> 2 -> 5 visited
9Return to parent1back to 14 -> 2 -> 5 visited
10Visit node1root4 -> 2 -> 5 -> 1 visited
11Traverse right subtree3right of 14 -> 2 -> 5 -> 1 visited
12Traverse left subtree6left of 34 -> 2 -> 5 -> 1 visited
13Visit node6left leaf4 -> 2 -> 5 -> 1 -> 6 visited
14Return to parent3back to 34 -> 2 -> 5 -> 1 -> 6 visited
15Visit node3right child4 -> 2 -> 5 -> 1 -> 6 -> 3 visited
16Traverse right subtree7right of 34 -> 2 -> 5 -> 1 -> 6 -> 3 visited
17Visit node7right leaf4 -> 2 -> 5 -> 1 -> 6 -> 3 -> 7 visited
18Traversal completenullend4 -> 2 -> 5 -> 1 -> 6 -> 3 -> 7 visited
💡 All nodes visited in left-root-right order, traversal ends.
Variable Tracker
VariableStartAfter Step 4After Step 6After Step 8After Step 10After Step 13After Step 15After Step 17Final
Current Node14251637null
Visited Nodes[][4][4,2][4,2,5][4,2,5,1][4,2,5,1,6][4,2,5,1,6,3][4,2,5,1,6,3,7][4,2,5,1,6,3,7]
Key Moments - 3 Insights
Why do we visit the left subtree before the root node?
In the execution_table at steps 2-4, the traversal goes deep into the left subtree first, ensuring all left children are visited before the root, following the left-root-right order.
What happens when a node has no left child?
At step 4 and step 8, when the node has no left child, the traversal visits the node itself immediately, as shown by visiting node 4 and node 5.
How does the traversal know when to stop?
At step 18, the traversal ends when all nodes have been visited and the recursive calls return null, indicating no more nodes to visit.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the current node at step 10?
ANode 3
BNode 2
CNode 1
DNode 5
💡 Hint
Check the 'Current Node' variable in variable_tracker after step 10.
At which step does the traversal visit node 6?
AStep 12
BStep 13
CStep 15
DStep 17
💡 Hint
Look at the 'Operation' and 'Node Visited' columns in execution_table for node 6.
If the left subtree of node 2 was empty, which step would be skipped?
AStep 3
BStep 4
CStep 6
DStep 7
💡 Hint
Step 3 shows traversal into left subtree of node 2; if empty, this step would not occur.
Concept Snapshot
Inorder Tree Traversal:
- Visit left subtree recursively
- Visit root node
- Visit right subtree recursively

Use recursion or stack
Order: Left → Root → Right
Common for sorted output of BST
Full Transcript
Inorder traversal visits nodes in the order left subtree, root, then right subtree. Starting at the root, it recursively goes to the left child until it reaches a leaf. Then it visits that node, returns to the parent, visits it, and then traverses the right child. This process repeats until all nodes are visited. The execution table shows each step visiting nodes 4, 2, 5, 1, 6, 3, and 7 in that order. Variables track the current node and the list of visited nodes. Key moments clarify why left subtree is visited first, what happens when no left child exists, and when traversal ends. The visual quiz tests understanding of node visits at specific steps and effects of missing subtrees.