0
0
DSA C++programming~20 mins

Create a Binary Tree Manually in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Binary Tree Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Manual Binary Tree Creation and Inorder Traversal
What is the output of the inorder traversal after manually creating this binary tree?
DSA C++
struct Node {
    int data;
    Node* left;
    Node* right;
    Node(int val) : data(val), left(nullptr), right(nullptr) {}
};

Node* root = new Node(10);
root->left = new Node(5);
root->right = new Node(15);
root->left->left = new Node(3);
root->left->right = new Node(7);

void inorder(Node* node) {
    if (!node) return;
    inorder(node->left);
    std::cout << node->data << " ";
    inorder(node->right);
}

inorder(root);
A15 10 7 5 3
B10 5 3 7 15
C3 5 7 10 15
D5 3 7 10 15
Attempts:
2 left
💡 Hint
Inorder traversal visits left subtree, then node, then right subtree.
🧠 Conceptual
intermediate
1:00remaining
Number of Nodes in a Manually Created Binary Tree
If you manually create a binary tree with root node and add two children to the root, and then add one child to the left child, how many nodes does the tree have?
A4
B5
C3
D2
Attempts:
2 left
💡 Hint
Count the root, its two children, and the one child added to the left child.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Manual Binary Tree Node Linking
What error will occur when running this code snippet that manually creates a binary tree?
DSA C++
struct Node {
    int data;
    Node* left;
    Node* right;
    Node(int val) : data(val), left(nullptr), right(nullptr) {}
};

Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);

// Mistake: Trying to access right child of a nullptr
std::cout << root->right->left->data << std::endl;
ANo output, program runs fine
BCompilation error: missing semicolon
COutput: 0
DSegmentation fault (accessing nullptr)
Attempts:
2 left
💡 Hint
Check if root->right->left is initialized before accessing.
🚀 Application
advanced
2:00remaining
Manual Binary Tree Height Calculation
Given a manually created binary tree, what is the height of the tree?
DSA C++
struct Node {
    int data;
    Node* left;
    Node* right;
    Node(int val) : data(val), left(nullptr), right(nullptr) {}
};

Node* root = new Node(8);
root->left = new Node(3);
root->right = new Node(10);
root->left->left = new Node(1);
root->left->right = new Node(6);
root->left->right->left = new Node(4);
root->right->right = new Node(14);
root->right->right->left = new Node(13);

int height(Node* node) {
    if (!node) return 0;
    int leftHeight = height(node->left);
    int rightHeight = height(node->right);
    return 1 + (leftHeight > rightHeight ? leftHeight : rightHeight);
}

int h = height(root);
std::cout << h << std::endl;
A3
B4
C5
D2
Attempts:
2 left
💡 Hint
Height is the longest path from root to leaf node.
🧠 Conceptual
expert
1:30remaining
Number of Null Pointers in a Complete Binary Tree of Height 3
How many null pointers are there in a complete binary tree of height 3 (height counted as number of nodes on longest path from root to leaf)?
A8
B7
C4
D15
Attempts:
2 left
💡 Hint
A complete binary tree of height h has 2^h - 1 nodes. Number of null pointers = number of missing children.