0
0
DSA C++programming~20 mins

Check if Binary Tree is Balanced in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
šŸŽ–ļø
Balanced Tree Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā“ Predict Output
intermediate
2:00remaining
Output of height check in balanced binary tree
What is the output of the following C++ code that checks if a binary tree is balanced? The code prints 1 if balanced, 0 if not.
DSA C++
struct Node {
    int val;
    Node* left;
    Node* right;
    Node(int x) : val(x), left(nullptr), right(nullptr) {}
};

int height(Node* root) {
    if (!root) return 0;
    int lh = height(root->left);
    if (lh == -1) return -1;
    int rh = height(root->right);
    if (rh == -1) return -1;
    if (abs(lh - rh) > 1) return -1;
    return 1 + std::max(lh, rh);
}

bool isBalanced(Node* root) {
    return height(root) != -1;
}

int main() {
    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);
    root->right->right = new Node(6);
    root->left->left->left = new Node(7);
    std::cout << isBalanced(root) << std::endl;
    return 0;
}
A1
B0
CCompilation error
DRuntime error
Attempts:
2 left
šŸ’” Hint
Check the height difference between left and right subtrees at each node.
🧠 Conceptual
intermediate
1:00remaining
Understanding balanced binary tree condition
Which of the following best describes the condition for a binary tree to be balanced?
AAll leaf nodes are at the same depth
BThe tree has the same number of nodes on left and right subtrees
CFor every node, the height difference between left and right subtrees is at most 1
DThe tree is a complete binary tree
Attempts:
2 left
šŸ’” Hint
Balance depends on height difference, not node count or completeness.
šŸ”§ Debug
advanced
2:00remaining
Identify the bug in height calculation for balance check
What error does the following code produce when checking if a binary tree is balanced?
DSA C++
int height(Node* root) {
    if (!root) return 0;
    int lh = height(root->left);
    int rh = height(root->right);
    if (abs(lh - rh) > 1) return -1;
    return 1 + std::max(lh, rh);
}

bool isBalanced(Node* root) {
    return height(root) != -1;
}
ACompilation error due to missing header
BCauses infinite recursion
CReturns -1 for balanced trees
DReturns incorrect result for unbalanced trees
Attempts:
2 left
šŸ’” Hint
Check if the function propagates the -1 result from subtrees.
ā“ Predict Output
advanced
2:00remaining
Output of balance check on a perfect binary tree
What is the output of the following code that checks if a perfect binary tree is balanced?
DSA C++
struct Node {
    int val;
    Node* left;
    Node* right;
    Node(int x) : val(x), left(nullptr), right(nullptr) {}
};

int height(Node* root) {
    if (!root) return 0;
    int lh = height(root->left);
    if (lh == -1) return -1;
    int rh = height(root->right);
    if (rh == -1) return -1;
    if (abs(lh - rh) > 1) return -1;
    return 1 + std::max(lh, rh);
}

bool isBalanced(Node* root) {
    return height(root) != -1;
}

int main() {
    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);
    root->right->left = new Node(6);
    root->right->right = new Node(7);
    std::cout << isBalanced(root) << std::endl;
    return 0;
}
A1
BRuntime error
CCompilation error
D0
Attempts:
2 left
šŸ’” Hint
A perfect binary tree is always balanced.
šŸš€ Application
expert
3:00remaining
Number of balanced subtrees in a binary tree
Given a binary tree, how many subtrees (including single nodes) are balanced? Consider the tree below: Root(1) ā”œā”€ Left(2) │ ā”œā”€ Left(4) │ └─ Right(5) └─ Right(3) └─ Right(6) └─ Left(7) How many balanced subtrees does this tree have?
A7
B6
C5
D8
Attempts:
2 left
šŸ’” Hint
Count all single nodes and check balance for larger subtrees.