Bird
Raised Fist0
DSA Cprogramming~20 mins

Binary Tree Node Structure in DSA C - Practice Problems & Challenges

Choose your learning style10 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 inserting nodes in a binary tree
What is the printed output of the binary tree after inserting nodes 10, 5, 15 in that order using the given insert function?
DSA C
typedef struct Node {
    int data;
    struct Node* left;
    struct Node* right;
} Node;

Node* insert(Node* root, int value) {
    if (root == NULL) {
        Node* newNode = (Node*)malloc(sizeof(Node));
        newNode->data = value;
        newNode->left = NULL;
        newNode->right = NULL;
        return newNode;
    }
    if (value < root->data) {
        root->left = insert(root->left, value);
    } else {
        root->right = insert(root->right, value);
    }
    return root;
}

void printInOrder(Node* root) {
    if (root == NULL) return;
    printInOrder(root->left);
    printf("%d -> ", root->data);
    printInOrder(root->right);
}

int main() {
    Node* root = NULL;
    root = insert(root, 10);
    root = insert(root, 5);
    root = insert(root, 15);
    printInOrder(root);
    printf("null\n");
    return 0;
}
A5 -> 10 -> 15 -> null
B10 -> 5 -> 15 -> null
Cnull
D15 -> 10 -> 5 -> null
Attempts:
2 left
💡 Hint
In-order traversal prints nodes in ascending order for a binary search tree.
🧠 Conceptual
intermediate
1:00remaining
Understanding binary tree node pointers
In a binary tree node structure, what do the 'left' and 'right' pointers represent?
APointers to the node's parent and sibling nodes
BPointers to the next and previous nodes in a linked list
CPointers to the node's left and right child nodes
DPointers to the node's data and metadata
Attempts:
2 left
💡 Hint
Think about how a binary tree branches out from each node.
🔧 Debug
advanced
2:00remaining
Identify the error in binary tree node initialization
What error will occur when running this code snippet for creating a new binary tree node?
DSA C
#include <stdlib.h>

Node* createNode(int value) {
    Node* newNode = malloc(sizeof(Node));
    newNode->data = value;
    newNode->left = NULL;
    newNode->right = NULL;
    return newNode;
}

int main() {
    Node* root = createNode(10);
    printf("%d\n", root->data);
    return 0;
}
ACompilation error due to missing #include <stdlib.h>
BRuntime error due to uninitialized pointers
CCompilation error due to missing semicolon after struct definition
DNo error, code runs and prints 10
Attempts:
2 left
💡 Hint
malloc requires a specific header file to be included.
❓ Predict Output
advanced
2:00remaining
Output of pre-order traversal on a binary tree
Given the binary tree constructed by inserting 8, 3, 10, 1, 6, 14, what is the output of the pre-order traversal?
DSA C
typedef struct Node {
    int data;
    struct Node* left;
    struct Node* right;
} Node;

Node* insert(Node* root, int value) {
    if (root == NULL) {
        Node* newNode = (Node*)malloc(sizeof(Node));
        newNode->data = value;
        newNode->left = NULL;
        newNode->right = NULL;
        return newNode;
    }
    if (value < root->data) {
        root->left = insert(root->left, value);
    } else {
        root->right = insert(root->right, value);
    }
    return root;
}

void printPreOrder(Node* root) {
    if (root == NULL) return;
    printf("%d -> ", root->data);
    printPreOrder(root->left);
    printPreOrder(root->right);
}

int main() {
    Node* root = NULL;
    int values[] = {8, 3, 10, 1, 6, 14};
    for (int i = 0; i < 6; i++) {
        root = insert(root, values[i]);
    }
    printPreOrder(root);
    printf("null\n");
    return 0;
}
A8 -> 3 -> 1 -> 6 -> 10 -> 14 -> null
B1 -> 3 -> 6 -> 8 -> 10 -> 14 -> null
C14 -> 10 -> 8 -> 6 -> 3 -> 1 -> null
D3 -> 1 -> 6 -> 8 -> 10 -> 14 -> null
Attempts:
2 left
💡 Hint
Pre-order traversal visits root first, then left subtree, then right subtree.
🧠 Conceptual
expert
1:30remaining
Maximum nodes in a binary tree of height h
What is the maximum number of nodes in a binary tree of height h (where height is the number of edges on the longest path from root to leaf)?
A2^h - 1
B2^(h+1) - 1
Ch^2
Dh * 2
Attempts:
2 left
💡 Hint
Think about a perfect binary tree where every level is fully filled.

Practice

(1/5)
1. What does a typical binary tree node structure contain in C?
easy
A. Two data fields and no pointers
B. Only a data field without any pointers
C. A data field and a pointer to the parent node only
D. A data field and two pointers to left and right child nodes

Solution

  1. Step 1: Understand binary tree node components

    A binary tree node must hold data and links to its children to form the tree structure.
  2. Step 2: Identify correct structure

    The node typically has one data field and two pointers: one for the left child and one for the right child.
  3. Final Answer:

    A data field and two pointers to left and right child nodes -> Option D
  4. Quick Check:

    Binary tree node = data + left + right pointers [OK]
Hint: Remember: binary tree nodes have two child pointers [OK]
Common Mistakes:
  • Thinking nodes have no pointers
  • Confusing parent pointer with child pointers
  • Assuming multiple data fields
2. Which of the following is the correct C syntax to define a binary tree node structure?
easy
A. struct Node { int data; struct Node left; struct Node right; };
B. struct Node { int data; Node* left; Node* right; };
C. struct Node { int data; struct Node* left; struct Node* right; };
D. struct Node { int data; struct Node* parent; };

Solution

  1. Step 1: Check pointer usage in struct

    In C, pointers to the same struct type must be declared with 'struct Node*' inside the struct.
  2. Step 2: Identify correct syntax

    struct Node { int data; struct Node* left; struct Node* right; }; correctly uses 'struct Node*' for left and right child pointers.
  3. Final Answer:

    struct Node { int data; struct Node* left; struct Node* right; }; -> Option C
  4. Quick Check:

    Use 'struct Node*' for pointers inside struct [OK]
Hint: Use 'struct Node*' for pointers inside struct in C [OK]
Common Mistakes:
  • Omitting 'struct' keyword before Node*
  • Using struct Node instead of pointer causing infinite size
  • Confusing parent pointer with child pointers
3. Given the following code, what will be printed?
struct Node {
  int data;
  struct Node* left;
  struct Node* right;
};

int main() {
  struct Node n1 = {10, NULL, NULL};
  struct Node n2 = {20, NULL, NULL};
  struct Node root = {5, &n1, &n2};
  printf("%d %d %d", root.data, root.left->data, root.right->data);
  return 0;
}
medium
A. 5 10 20
B. 10 5 20
C. 5 0 0
D. Compilation error

Solution

  1. Step 1: Understand node initialization

    n1 has data 10, n2 has data 20, root has data 5 with left pointing to n1 and right pointing to n2.
  2. Step 2: Trace printf output

    root.data is 5, root.left->data is 10, root.right->data is 20, so output is '5 10 20'.
  3. Final Answer:

    5 10 20 -> Option A
  4. Quick Check:

    Print root and children data = 5 10 20 [OK]
Hint: Follow pointers to child nodes for data [OK]
Common Mistakes:
  • Confusing data values of nodes
  • Assuming NULL pointers print zero
  • Mixing order of print arguments
4. Identify the error in this binary tree node definition:
struct Node {
  int data;
  struct Node left;
  struct Node right;
};
medium
A. Left and right should be pointers, not structs
B. Missing semicolon after struct definition
C. Data field should be a pointer
D. Struct name should be lowercase

Solution

  1. Step 1: Analyze struct member types

    Left and right are declared as struct Node, which causes infinite size because each contains two more structs recursively.
  2. Step 2: Correct pointer usage

    They should be pointers (struct Node*) to avoid infinite recursion and allow linking nodes.
  3. Final Answer:

    Left and right should be pointers, not structs -> Option A
  4. Quick Check:

    Child nodes must be pointers to avoid infinite size [OK]
Hint: Use pointers for child nodes to prevent infinite size [OK]
Common Mistakes:
  • Declaring child nodes as structs instead of pointers
  • Ignoring compiler errors about struct size
  • Thinking data field needs to be pointer
5. You want to create a binary tree node in C that stores a character and has pointers to left and right children. Which of the following struct definitions correctly achieves this and allows dynamic node creation?
hard
A. struct Node { char data; struct Node left; struct Node right; }; struct Node* createNode(char c) { struct Node newNode; newNode.data = c; newNode.left = NULL; newNode.right = NULL; return &newNode; }
B. struct Node { char data; struct Node* left; struct Node* right; }; struct Node* createNode(char c) { struct Node* newNode = malloc(sizeof(struct Node)); newNode->data = c; newNode->left = NULL; newNode->right = NULL; return newNode; }
C. struct Node { char data; struct Node* left; struct Node* right; }; struct Node createNode(char c) { struct Node newNode; newNode.data = c; newNode.left = NULL; newNode.right = NULL; return newNode; }
D. struct Node { char data; struct Node* left; struct Node* right; }; struct Node* createNode(char c) { struct Node newNode; newNode.data = c; newNode.left = NULL; newNode.right = NULL; return newNode; }

Solution

  1. Step 1: Check struct definition

    struct Node { char data; struct Node* left; struct Node* right; }; struct Node* createNode(char c) { struct Node* newNode = malloc(sizeof(struct Node)); newNode->data = c; newNode->left = NULL; newNode->right = NULL; return newNode; } correctly defines pointers for left and right children and uses char data type.
  2. Step 2: Verify dynamic node creation

    struct Node { char data; struct Node* left; struct Node* right; }; struct Node* createNode(char c) { struct Node* newNode = malloc(sizeof(struct Node)); newNode->data = c; newNode->left = NULL; newNode->right = NULL; return newNode; } uses malloc to allocate memory dynamically and initializes pointers to NULL, returning a pointer to the new node.
  3. Final Answer:

    Correct struct with pointers and dynamic allocation using malloc -> Option B
  4. Quick Check:

    Dynamic node creation requires malloc and pointer returns [OK]
Hint: Use malloc and pointers for dynamic binary tree nodes [OK]
Common Mistakes:
  • Returning address of local variable (invalid after function ends)
  • Using struct members instead of pointers for children
  • Not initializing child pointers to NULL
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