Bird
Raised Fist0
DSA C++programming~10 mins

Tree Traversal Preorder Root Left Right in DSA C++ - Execution Trace

Choose your learning style10 modes available
Concept Flow - Tree Traversal Preorder Root Left Right
Start at Root Node
↓
Visit Node: Process Data
↓
Traverse Left Subtree
↓
Traverse Right Subtree
↓
Done
Preorder traversal visits the root node first, then recursively visits the left subtree, followed by the right subtree.
Execution Sample
DSA C++
struct Node {
  int data;
  Node* left;
  Node* right;
};

void preorder(Node* root) {
  if (!root) return;
  std::cout << root->data << " ";
  preorder(root->left);
  preorder(root->right);
}
This code prints nodes in preorder: root first, then left subtree, then right subtree.
Execution Table
StepOperationNode VisitedLeft/RightTree StatePointer ChangesVisual State
1Start at root1Root1 / \ 2 3current = root(1)1 / \ 2 3
2Visit node1Root1 / \ 2 3print 11 / \ 2 3
3Traverse left subtree2Left1 / \ 2 3current = 21 / \ 2 3
4Visit node2Left1 / \ 2 3print 21 / \ 2 3
5Traverse left subtree4Left of 21 / \ 2 3 / 4current = 41 / \ 2 3 / 4
6Visit node4Left of 21 / \ 2 3 / 4print 41 / \ 2 3 / 4
7Traverse left subtreenullLeft of 41 / \ 2 3 / 4current = null1 / \ 2 3 / 4
8Return from nullnullLeft of 41 / \ 2 3 / 4return1 / \ 2 3 / 4
9Traverse right subtreenullRight of 41 / \ 2 3 / 4current = null1 / \ 2 3 / 4
10Return from nullnullRight of 41 / \ 2 3 / 4return1 / \ 2 3 / 4
11Return to node 22Left1 / \ 2 3 / 4current = 21 / \ 2 3 / 4
12Traverse right subtree5Right of 21 / \ 2 3 / \ 4 5current = 51 / \ 2 3 / \ 4 5
13Visit node5Right of 21 / \ 2 3 / \ 4 5print 51 / \ 2 3 / \ 4 5
14Traverse left subtreenullLeft of 51 / \ 2 3 / \ 4 5current = null1 / \ 2 3 / \ 4 5
15Return from nullnullLeft of 51 / \ 2 3 / \ 4 5return1 / \ 2 3 / \ 4 5
16Traverse right subtreenullRight of 51 / \ 2 3 / \ 4 5current = null1 / \ 2 3 / \ 4 5
17Return from nullnullRight of 51 / \ 2 3 / \ 4 5return1 / \ 2 3 / \ 4 5
18Return to node 11Root1 / \ 2 3 / \ 4 5current = 11 / \ 2 3 / \ 4 5
19Traverse right subtree3Right1 / \ 2 3 / \ 4 5current = 31 / \ 2 3 / \ 4 5
20Visit node3Right1 / \ 2 3 / \ 4 5print 31 / \ 2 3 / \ 4 5
21Traverse left subtreenullLeft of 31 / \ 2 3 / \ 4 5current = null1 / \ 2 3 / \ 4 5
22Return from nullnullLeft of 31 / \ 2 3 / \ 4 5return1 / \ 2 3 / \ 4 5
23Traverse right subtreenullRight of 31 / \ 2 3 / \ 4 5current = null1 / \ 2 3 / \ 4 5
24Return from nullnullRight of 31 / \ 2 3 / \ 4 5return1 / \ 2 3 / \ 4 5
25Traversal completenullNone1 / \ 2 3 / \ 4 5return1 / \ 2 3 / \ 4 5
💡 All nodes visited in preorder: root, left subtree, right subtree; recursion ends when null nodes reached
Variable Tracker
VariableStartAfter Step 3After Step 12After Step 19Final
currentroot(1)node(2)node(5)node(3)null
print_output"""1 2""1 2 4 5""1 2 4 5 3""1 2 4 5 3"
Key Moments - 3 Insights
Why do we print the node's data before traversing its children?
Because preorder traversal visits the root node first, as shown in execution_table rows 2, 4, 6, 13, and 20 where printing happens before recursive calls.
Why do we return immediately when the current node is null?
Returning on null nodes stops recursion, preventing errors and infinite loops, as seen in rows 7, 9, 14, 16, 21, and 23 where traversal returns without action.
How does the traversal know when to move from left subtree to right subtree?
After finishing left subtree traversal (returning from null), the code proceeds to right subtree calls, visible in execution_table rows 11 to 12 and 18 to 19.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what node is visited at step 13?
ANode 5
BNode 4
CNode 3
DNode 2
💡 Hint
Check the 'Node Visited' column at step 13 in execution_table
At which step does the traversal move from left subtree of node 2 to its right subtree?
AStep 11
BStep 12
CStep 7
DStep 18
💡 Hint
Look for the step where 'Traverse right subtree' of node 2 starts in execution_table
If the tree had no right child for node 1, how would the traversal end?
ATraversal would skip left subtree
BTraversal would continue infinitely
CTraversal ends after visiting left subtree nodes
DTraversal would print only root node
💡 Hint
Refer to variable_tracker and execution_table showing traversal stops when null nodes reached
Concept Snapshot
Preorder Tree Traversal (Root, Left, Right):
- Visit root node first (print/process)
- Recursively traverse left subtree
- Recursively traverse right subtree
- Stops when node is null
- Prints nodes in root-left-right order
Full Transcript
Preorder traversal visits the root node first, then the left subtree, and finally the right subtree. The code starts at the root, prints its data, then recursively calls preorder on the left child, and then on the right child. When a null node is reached, the recursion returns immediately. This process continues until all nodes are visited in preorder sequence. The example tree with nodes 1, 2, 3, 4, and 5 is traversed in the order 1 2 4 5 3.

Practice

(1/5)
1. What is the order of nodes visited in a preorder tree traversal?
easy
A. Right, Root, Left
B. Left, Root, Right
C. Left, Right, Root
D. Root, Left, Right

Solution

  1. Step 1: Understand preorder traversal definition

    Preorder traversal visits the root node first, then the left subtree, and finally the right subtree.
  2. Step 2: Match the order with options

    Root, Left, Right matches the order Root, Left, Right exactly.
  3. Final Answer:

    Root, Left, Right -> Option D
  4. Quick Check:

    Preorder = Root, Left, Right [OK]
Hint: Remember preorder starts with the root node first [OK]
Common Mistakes:
  • Confusing preorder with inorder or postorder
  • Visiting left before root
  • Visiting right before left
2. Which of the following C++ function signatures correctly represents a preorder traversal of a binary tree node Node*?
easy
A. void preorder(Node* root) { if(root) { cout << root->data; preorder(root->left); preorder(root->right); } }
B. void preorder(Node* root) { if(root) { preorder(root->left); preorder(root->right); cout << root->data; } }
C. void preorder(Node* root) { if(root) { preorder(root->right); cout << root->data; preorder(root->left); } }
D. void preorder(Node* root) { if(root) { preorder(root->left); cout << root->data; preorder(root->right); } }

Solution

  1. Step 1: Recall preorder traversal order

    Preorder visits root first, then left subtree, then right subtree.
  2. Step 2: Check function code order

    void preorder(Node* root) { if(root) { cout << root->data; preorder(root->left); preorder(root->right); } } prints root data first, then recursively visits left and right children, matching preorder.
  3. Final Answer:

    void preorder(Node* root) { if(root) { cout << root->data; preorder(root->left); preorder(root->right); } } -> Option A
  4. Quick Check:

    Print root first, then left, then right [OK]
Hint: Print root data before recursive calls for preorder [OK]
Common Mistakes:
  • Printing after recursive calls (postorder)
  • Swapping left and right recursive calls
  • Missing null check for root
3. Given the binary tree below, what is the preorder traversal output?

      10
     /  \
    5    15
   /    /  \
  3    12  20
medium
A. [10, 15, 20, 12, 5, 3]
B. [10, 5, 3, 15, 12, 20]
C. [3, 5, 10, 12, 15, 20]
D. [5, 3, 10, 12, 15, 20]

Solution

  1. Step 1: Start at root and visit nodes in preorder

    Visit root 10, then left subtree (5, then 3), then right subtree (15, then 12 and 20).
  2. Step 2: Write traversal sequence

    Sequence is 10, 5, 3, 15, 12, 20.
  3. Final Answer:

    [10, 5, 3, 15, 12, 20] -> Option B
  4. Quick Check:

    Preorder = Root, Left, Right = [10,5,3,15,12,20] [OK]
Hint: Follow root, then left subtree, then right subtree order [OK]
Common Mistakes:
  • Mixing preorder with inorder or postorder
  • Visiting right subtree before left
  • Skipping leaf nodes
4. Identify the error in the following preorder traversal function and select the corrected version:

void preorder(Node* root) {
  if(root == nullptr) return;
  preorder(root->left);
  cout << root->data << " ";
  preorder(root->right);
}
medium
A. void preorder(Node* root) { if(root == nullptr) return; cout << root->data << " "; preorder(root->left); preorder(root->right); }
B. void preorder(Node* root) { if(root == nullptr) return; preorder(root->right); cout << root->data << " "; preorder(root->left); }
C. void preorder(Node* root) { if(root == nullptr) return; preorder(root->left); preorder(root->right); cout << root->data << " "; }
D. void preorder(Node* root) { if(root == nullptr) return; cout << root->data << " "; preorder(root->right); preorder(root->left); }

Solution

  1. Step 1: Identify preorder traversal order

    Preorder visits root first, then left subtree, then right subtree.
  2. Step 2: Check given function and options

    The given function prints after left subtree, which is inorder traversal. void preorder(Node* root) { if(root == nullptr) return; cout << root->data << " "; preorder(root->left); preorder(root->right); } prints root first, then left, then right, matching preorder.
  3. Final Answer:

    void preorder(Node* root) { if(root == nullptr) return; cout << root->data << " "; preorder(root->left); preorder(root->right); } -> Option A
  4. Quick Check:

    Print root before recursive calls for preorder [OK]
Hint: Print root data before recursive calls to fix preorder [OK]
Common Mistakes:
  • Printing after left subtree (inorder)
  • Swapping left and right subtree calls
  • Missing null pointer check
5. You have a binary tree where some nodes have only one child. How does preorder traversal help in serializing this tree uniquely, and which sequence below represents the preorder traversal of this tree?

       8
      / \
     4   10
      \    \
       6    20
hard
A. [8, 10, 20, 4, 6]
B. [4, 6, 8, 10, 20]
C. [8, 4, 6, 10, 20]
D. [6, 4, 8, 20, 10]

Solution

  1. Step 1: Understand preorder traversal for serialization

    Preorder visits root first, then left subtree, then right subtree, capturing structure including nodes with single children.
  2. Step 2: Traverse given tree in preorder

    Visit 8 (root), then left subtree: 4, then 6 (right child of 4), then right subtree: 10, then 20 (right child of 10).
  3. Step 3: Write traversal sequence

    Sequence is [8, 4, 6, 10, 20].
  4. Final Answer:

    [8, 4, 6, 10, 20] -> Option C
  5. Quick Check:

    Preorder captures root-left-right including single children [OK]
Hint: Preorder captures tree shape by visiting root before children [OK]
Common Mistakes:
  • Ignoring nodes with single child
  • Mixing preorder with inorder
  • Visiting right subtree before left
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard