0
0
DSA C++programming~20 mins

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

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

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

void inorder(Node* root, std::vector<int>& result) {
    if (!root) return;
    inorder(root->left, result);
    result.push_back(root->val);
    inorder(root->right, result);
}

int main() {
    Node* root = new Node(2);
    root->left = new Node(1);
    root->right = new Node(3);
    std::vector<int> result;
    inorder(root, result);
    for (int v : result) std::cout << v << " ";
    return 0;
}
A[1, 2, 3]
B[2, 1, 3]
C[3, 2, 1]
D[1, 3, 2]
Attempts:
2 left
💡 Hint
Remember inorder traversal visits left child first, then root, then right child.
Predict Output
intermediate
2:00remaining
Inorder Traversal Output of a Larger Tree
What is the output of inorder traversal (Left, Root, Right) for this binary tree?

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

void inorder(Node* root, std::vector<int>& result) {
    if (!root) return;
    inorder(root->left, result);
    result.push_back(root->val);
    inorder(root->right, result);
}

int main() {
    Node* root = new Node(4);
    root->left = new Node(2);
    root->right = new Node(5);
    root->left->left = new Node(1);
    root->left->right = new Node(3);
    std::vector<int> result;
    inorder(root, result);
    for (int v : result) std::cout << v << " ";
    return 0;
}
A[2, 1, 3, 4, 5]
B[4, 2, 1, 3, 5]
C[5, 4, 3, 2, 1]
D[1, 2, 3, 4, 5]
Attempts:
2 left
💡 Hint
Inorder traversal visits left subtree fully before root, then right subtree.
🔧 Debug
advanced
2:00remaining
Identify the Error in Inorder Traversal Implementation
What error will this inorder traversal code cause when run on a non-empty tree?
DSA C++
void inorder(Node* root, std::vector<int>& result) {
    if (root == nullptr) return;
    inorder(root->right, result);
    result.push_back(root->val);
    inorder(root->left, result);
}
AThe code will not compile due to syntax error
BThe code will cause a segmentation fault
CThe output will be reversed inorder traversal (Right, Root, Left)
DThe output will be the same as correct inorder traversal
Attempts:
2 left
💡 Hint
Check the order of recursive calls and when the root value is added.
🧠 Conceptual
advanced
1:00remaining
Number of Nodes Visited in Inorder Traversal
Given a binary tree with 7 nodes, how many nodes will be visited during a complete inorder traversal?
A7
B8
C14
D6
Attempts:
2 left
💡 Hint
Inorder traversal visits every node exactly once.
Predict Output
expert
3:00remaining
Inorder Traversal Output of Unbalanced Tree
What is the output of inorder traversal (Left, Root, Right) for this unbalanced binary tree?

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

void inorder(Node* root, std::vector<int>& result) {
    if (!root) return;
    inorder(root->left, result);
    result.push_back(root->val);
    inorder(root->right, result);
}

int main() {
    Node* root = new Node(10);
    root->left = new Node(5);
    root->right = new Node(15);
    root->left->right = new Node(7);
    root->right->right = new Node(20);
    std::vector<int> result;
    inorder(root, result);
    for (int v : result) std::cout << v << " ";
    return 0;
}
A[10, 5, 7, 15, 20]
B[5, 7, 10, 15, 20]
C[7, 5, 10, 20, 15]
D[5, 10, 7, 15, 20]
Attempts:
2 left
💡 Hint
Follow left subtree fully, then root, then right subtree.