0
0
DSA C++programming~20 mins

Tree Traversal Preorder Root Left Right in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Preorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Preorder Traversal on a Simple Binary Tree
What is the output of the preorder traversal (Root, Left, Right) of the following binary tree?

Tree structure:
1
/ \
2 3
/ \
4 5
DSA C++
struct Node {
    int val;
    Node* left;
    Node* right;
    Node(int x) : val(x), left(nullptr), right(nullptr) {}
};

void preorder(Node* root) {
    if (!root) return;
    std::cout << root->val << " ";
    preorder(root->left);
    preorder(root->right);
}

int main() {
    Node* root = new Node(1);
    root->left = new Node(2);
    root->right = new Node(3);
    root->right->left = new Node(4);
    root->right->right = new Node(5);
    preorder(root);
    return 0;
}
A1 2 4 3 5
B1 3 4 5 2
C1 2 3 4 5
D2 1 4 3 5
Attempts:
2 left
💡 Hint
Remember preorder visits root first, then left subtree, then right subtree.
🧠 Conceptual
intermediate
1:00remaining
Number of Nodes Visited in Preorder Traversal
Given a binary tree with 7 nodes, how many nodes will be visited during a preorder traversal?
A8
B6
CDepends on the tree structure
D7
Attempts:
2 left
💡 Hint
Preorder traversal visits every node exactly once.
Predict Output
advanced
2:00remaining
Output of Preorder Traversal with Null Children
What is the output of the preorder traversal of this tree?

Tree structure:
10
/ \
null 20
/ \
30 null
DSA C++
struct Node {
    int val;
    Node* left;
    Node* right;
    Node(int x) : val(x), left(nullptr), right(nullptr) {}
};

void preorder(Node* root) {
    if (!root) return;
    std::cout << root->val << " ";
    preorder(root->left);
    preorder(root->right);
}

int main() {
    Node* root = new Node(10);
    root->right = new Node(20);
    root->right->left = new Node(30);
    preorder(root);
    return 0;
}
A10 20 30
B10 30 20
C20 10 30
D10 20
Attempts:
2 left
💡 Hint
Preorder visits root, then left, then right. Null children are skipped.
🔧 Debug
advanced
1:30remaining
Identify the Error in Preorder Traversal Code
What error will this preorder traversal code produce when run on a non-empty tree?
DSA C++
void preorder(Node* root) {
    if (root == nullptr) return;
    preorder(root->left);
    std::cout << root->val << " ";
    preorder(root->right);
}
ACompilation error due to missing return type
BPrints nodes in inorder, not preorder
CPrints nodes in preorder correctly
DRuntime error due to null pointer dereference
Attempts:
2 left
💡 Hint
Check the order of printing and recursive calls.
🧠 Conceptual
expert
1:00remaining
Preorder Traversal Output for a Skewed Tree
Consider a binary tree where every node has only a right child, forming a chain of nodes with values 1 to 5. What is the preorder traversal output?
A2 1 3 4 5
B1 3 5 2 4
C1 2 3 4 5
D5 4 3 2 1
Attempts:
2 left
💡 Hint
Preorder visits root first, then left (none), then right.