Challenge - 5 Problems
Balanced Tree Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Balanced Check on Simple Tree
What is the output of the following C# code that checks if a binary tree is balanced?
DSA C#
using System; class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } class Program { static int CheckHeight(Node root) { if (root == null) return 0; int leftHeight = CheckHeight(root.left); if (leftHeight == -1) return -1; int rightHeight = CheckHeight(root.right); if (rightHeight == -1) return -1; if (Math.Abs(leftHeight - rightHeight) > 1) return -1; return Math.Max(leftHeight, rightHeight) + 1; } static bool IsBalanced(Node root) { return CheckHeight(root) != -1; } static void 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); Console.WriteLine(IsBalanced(root)); } }
Attempts:
2 left
💡 Hint
Think about the height difference between left and right subtrees at each node.
✗ Incorrect
The tree is balanced because for every node, the height difference between left and right subtrees is at most 1.
❓ Predict Output
intermediateOutput for Unbalanced Tree Check
What is the output of this C# code that checks if a binary tree is balanced?
DSA C#
using System; class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } class Program { static int CheckHeight(Node root) { if (root == null) return 0; int leftHeight = CheckHeight(root.left); if (leftHeight == -1) return -1; int rightHeight = CheckHeight(root.right); if (rightHeight == -1) return -1; if (Math.Abs(leftHeight - rightHeight) > 1) return -1; return Math.Max(leftHeight, rightHeight) + 1; } static bool IsBalanced(Node root) { return CheckHeight(root) != -1; } static void Main() { Node root = new Node(1); root.left = new Node(2); root.left.left = new Node(3); root.left.left.left = new Node(4); Console.WriteLine(IsBalanced(root)); } }
Attempts:
2 left
💡 Hint
Check the height difference at the root node.
✗ Incorrect
The tree is unbalanced because the left subtree is much deeper than the right subtree (which is null).
🧠 Conceptual
advancedUnderstanding the Balance Check Algorithm
Which statement best describes why the CheckHeight function returns -1?
Attempts:
2 left
💡 Hint
Think about what the special value -1 signals in the recursion.
✗ Incorrect
Returning -1 signals that the subtree is unbalanced, so the algorithm can stop checking further.
🔧 Debug
advancedIdentify the Bug in Balance Check Code
What error will this code produce when checking if a binary tree is balanced?
DSA C#
using System; class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } class Program { static int CheckHeight(Node root) { if (root == null) return 0; int leftHeight = CheckHeight(root.left); int rightHeight = CheckHeight(root.right); if (Math.Abs(leftHeight - rightHeight) > 1) return -1; return Math.Max(leftHeight, rightHeight) + 1; } static bool IsBalanced(Node root) { return CheckHeight(root) != -1; } static void Main() { Node root = new Node(1); root.left = new Node(2); root.left.left = new Node(3); root.left.left.left = new Node(4); Console.WriteLine(IsBalanced(root)); } }
Attempts:
2 left
💡 Hint
Check if the function stops checking after detecting unbalance.
✗ Incorrect
The code does not check if leftHeight or rightHeight is -1 before continuing, so it misses unbalanced subtrees.
🚀 Application
expertCount Balanced Subtrees in a Binary Tree
Given a binary tree, which code snippet correctly counts how many subtrees are balanced?
DSA C#
using System; class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } class Program { static int balancedCount = 0; static int CheckHeightAndCount(Node root) { if (root == null) return 0; int leftHeight = CheckHeightAndCount(root.left); if (leftHeight == -1) return -1; int rightHeight = CheckHeightAndCount(root.right); if (rightHeight == -1) return -1; if (Math.Abs(leftHeight - rightHeight) > 1) return -1; balancedCount++; return Math.Max(leftHeight, rightHeight) + 1; } static int CountBalancedSubtrees(Node root) { balancedCount = 0; CheckHeightAndCount(root); return balancedCount; } static void 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.left.left.left = new Node(6); Console.WriteLine(CountBalancedSubtrees(root)); } }
Attempts:
2 left
💡 Hint
Count every subtree that is balanced including leaf nodes.
✗ Incorrect
The code counts all balanced subtrees. The tree has 5 balanced subtrees including leaves and some internal nodes.
