0
0
DSA C++programming~10 mins

Tree Traversal Preorder Root Left Right in DSA C++ - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the root node's value first in preorder traversal.

DSA C++
void preorder(Node* root) {
    if (root == nullptr) return;
    std::cout << root->[1] << " ";
    preorder(root->left);
    preorder(root->right);
}
Drag options to blanks, or click blank then click option'
Adata
Bval
Cvalue
Dkey
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'val' or 'key' which may not exist in the Node structure.
Trying to print the pointer instead of the value.
2fill in blank
medium

Complete the code to recursively traverse the left subtree in preorder.

DSA C++
void preorder(Node* root) {
    if (root == nullptr) return;
    std::cout << root->data << " ";
    preorder(root->[1]);
    preorder(root->right);
}
Drag options to blanks, or click blank then click option'
Achild
Bright
Cnext
Dleft
Attempts:
3 left
💡 Hint
Common Mistakes
Calling preorder on 'right' before 'left'.
Using non-existent members like 'child' or 'next'.
3fill in blank
hard

Fix the error in the preorder traversal to correctly traverse the right subtree.

DSA C++
void preorder(Node* root) {
    if (root == nullptr) return;
    std::cout << root->data << " ";
    preorder(root->left);
    preorder(root->[1]);
}
Drag options to blanks, or click blank then click option'
Aleft
Bchild
Cright
Dnext
Attempts:
3 left
💡 Hint
Common Mistakes
Calling preorder on 'left' twice.
Using invalid member names like 'child' or 'next'.
4fill in blank
hard

Fill both blanks to complete the preorder traversal function that prints nodes in Root-Left-Right order.

DSA C++
void preorder(Node* root) {
    if (root == nullptr) return;
    std::cout << root->[1] << " ";
    preorder(root->[2]);
    preorder(root->right);
}
Drag options to blanks, or click blank then click option'
Adata
Bval
Cleft
Dright
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping 'left' and 'right' in traversal order.
Using 'val' instead of 'data' for the node's value.
5fill in blank
hard

Fill all three blanks to complete the preorder traversal that prints root, left subtree, then right subtree.

DSA C++
void preorder(Node* root) {
    if (root == nullptr) return;
    std::cout << root->[1] << " ";
    preorder(root->[2]);
    preorder(root->[3]);
}
Drag options to blanks, or click blank then click option'
Adata
Bleft
Cright
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of left and right traversal.
Using 'val' instead of 'data' for the node's value.