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 Binary Tree Check
What is the output of the following Java code that checks if a binary tree is balanced?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int val) { this.val = val; } } public class Main { public static boolean isBalanced(TreeNode root) { return checkHeight(root) != -1; } private static int checkHeight(TreeNode node) { if (node == null) return 0; int leftHeight = checkHeight(node.left); if (leftHeight == -1) return -1; int rightHeight = checkHeight(node.right); if (rightHeight == -1) return -1; if (Math.abs(leftHeight - rightHeight) > 1) return -1; return Math.max(leftHeight, rightHeight) + 1; } public static void main(String[] args) { TreeNode root = new TreeNode(1); root.left = new TreeNode(2); root.right = new TreeNode(3); root.left.left = new TreeNode(4); root.left.left.left = new TreeNode(5); System.out.println(isBalanced(root)); } }
Attempts:
2 left
💡 Hint
Think about the height difference between left and right subtrees at each node.
✗ Incorrect
The tree is unbalanced because the left subtree of the root has a height difference greater than 1 compared to the right subtree. The method returns false.
❓ Predict Output
intermediateOutput of Balanced Check on Perfect Binary Tree
What will be printed when running this code that checks if a perfect binary tree is balanced?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int val) { this.val = val; } } public class Main { public static boolean isBalanced(TreeNode root) { return checkHeight(root) != -1; } private static int checkHeight(TreeNode node) { if (node == null) return 0; int leftHeight = checkHeight(node.left); if (leftHeight == -1) return -1; int rightHeight = checkHeight(node.right); if (rightHeight == -1) return -1; if (Math.abs(leftHeight - rightHeight) > 1) return -1; return Math.max(leftHeight, rightHeight) + 1; } public static void main(String[] args) { TreeNode root = new TreeNode(1); root.left = new TreeNode(2); root.right = new TreeNode(3); root.left.left = new TreeNode(4); root.left.right = new TreeNode(5); root.right.left = new TreeNode(6); root.right.right = new TreeNode(7); System.out.println(isBalanced(root)); } }
Attempts:
2 left
💡 Hint
A perfect binary tree is always balanced.
✗ Incorrect
All leaf nodes are at the same level and the height difference between left and right subtrees of every node is 0 or 1, so the tree is balanced.
🔧 Debug
advancedIdentify the error in this balanced tree check code
What error will this Java code produce when checking if a binary tree is balanced?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int val) { this.val = val; } } public class Main { public static boolean isBalanced(TreeNode root) { return checkHeight(root) != 0; } private static int checkHeight(TreeNode node) { if (node == null) return 0; int leftHeight = checkHeight(node.left); if (leftHeight == -1) return -1; int rightHeight = checkHeight(node.right); if (rightHeight == -1) return -1; if (Math.abs(leftHeight - rightHeight) > 1) return -1; return Math.max(leftHeight, rightHeight) + 1; } }
Attempts:
2 left
💡 Hint
Check the condition used to decide if the tree is balanced.
✗ Incorrect
The isBalanced method incorrectly compares checkHeight(root) != 0 instead of != -1. This causes wrong results because 0 means empty tree (balanced), but the method expects -1 for unbalanced.
🧠 Conceptual
advancedUnderstanding Balanced Binary Tree Definition
Which of the following best describes the condition for a binary tree to be balanced?
Attempts:
2 left
💡 Hint
Balance depends on subtree heights, not node counts.
✗ Incorrect
A balanced binary tree means for every node, the height difference between left and right subtrees is at most 1.
❓ Predict Output
expertOutput of Balanced Check on Large Unbalanced Tree
What is the output of this code that checks if a large unbalanced binary tree is balanced?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int val) { this.val = val; } } public class Main { public static boolean isBalanced(TreeNode root) { return checkHeight(root) != -1; } private static int checkHeight(TreeNode node) { if (node == null) return 0; int leftHeight = checkHeight(node.left); if (leftHeight == -1) return -1; int rightHeight = checkHeight(node.right); if (rightHeight == -1) return -1; if (Math.abs(leftHeight - rightHeight) > 1) return -1; return Math.max(leftHeight, rightHeight) + 1; } public static void main(String[] args) { TreeNode root = new TreeNode(1); TreeNode current = root; for (int i = 2; i <= 10; i++) { current.left = new TreeNode(i); current = current.left; } System.out.println(isBalanced(root)); } }
Attempts:
2 left
💡 Hint
A long chain on one side makes the tree unbalanced.
✗ Incorrect
The tree is a linked list going left only, so height difference at root is large, making it unbalanced.
