📖 Scenario: You are working on a program that manages a family tree. To keep the tree easy to search and update, you want to check if the tree is balanced. A balanced tree means the left and right branches of every node are about the same height, so it doesn't lean too much to one side.
🎯 Goal: Build a program that creates a simple binary tree, sets a limit for height difference, checks if the tree is balanced, and prints the result.
📋 What You'll Learn
Create a binary tree using nodes with value, left, and right properties
Create a variable maxAllowedDifference to set the maximum height difference allowed between left and right subtrees
Write a function isBalanced that checks if the binary tree is balanced based on maxAllowedDifference
Print true or false depending on whether the tree is balanced
💡 Why This Matters
🌍 Real World
Balanced trees are important in databases and search engines to keep data easy to find quickly.
💼 Career
Understanding tree balance helps in roles like software development, data engineering, and system design where efficient data structures matter.
Progress0 / 4 steps
1
Create the Binary Tree
Create a binary tree with a root node called root. The root should have value 10, a left child with value 5, and a right child with value 15. Each node should be an object with value, left, and right properties. The left and right children of the leaf nodes should be null.
DSA Javascript
Hint
Think of each node as a small box with three parts: value, left, and right. Leaf nodes have left and right as null.
2
Set Maximum Allowed Height Difference
Create a variable called maxAllowedDifference and set it to 1. This will be the maximum allowed difference in height between the left and right subtrees for the tree to be considered balanced.
DSA Javascript
Hint
This variable helps decide how much difference in height is okay between left and right branches.
3
Write the Function to Check if Tree is Balanced
Write a function called isBalanced that takes a node as input and returns true if the tree rooted at that node is balanced, or false otherwise. Use maxAllowedDifference to check the height difference between left and right subtrees. You can write a helper function inside isBalanced to calculate the height of a subtree.
DSA Javascript
Hint
Use a helper function to find height. Check difference at current node, then check left and right subtrees recursively.
4
Print if the Tree is Balanced
Write a console.log statement to print the result of calling isBalanced with root as the argument.
DSA Javascript
Hint
Call isBalanced(root) and print the result using console.log.
Practice
(1/5)
1. What does it mean for a binary tree to be balanced?
easy
A. The tree has the same number of nodes on left and right
B. All nodes have two children
C. The tree is a complete binary tree
D. The height difference between left and right subtrees of every node is at most 1
Solution
Step 1: Understand balance condition
A balanced binary tree means the height difference between left and right subtrees of every node is at most 1.
Step 2: Compare other options
Options A, B, and D describe other tree properties but not the balance condition.
Final Answer:
The height difference between left and right subtrees of every node is at most 1 -> Option D
Quick Check:
Balance means height difference ≤ 1 [OK]
Hint: Balance means subtree heights differ by no more than 1 [OK]
Common Mistakes:
Confusing balanced with complete tree
Thinking balance means equal nodes on both sides
Assuming all nodes must have two children
2. Which of the following JavaScript function signatures is correct to check if a binary tree is balanced?
easy
A. function checkBalance(tree) { ... }
B. function isBalanced(root) { ... }
C. function balancedCheck(node) { ... }
D. function isBalancedTree() { ... }
Solution
Step 1: Identify common parameter for tree root
The function should accept the root node as a parameter to check balance.
Step 2: Check function signatures
Only function isBalanced(root) { ... } uses a clear parameter named 'root' which is standard for tree root nodes.
Final Answer:
function isBalanced(root) { ... } -> Option B
Quick Check:
Function needs root parameter [OK]
Hint: Function must accept root node parameter [OK]
Common Mistakes:
Omitting root parameter
Using unclear or no parameters
Using function names that don't indicate balance check
3. What will be the output of the following code snippet?
class Node {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
function height(node) {
if (!node) return 0;
return 1 + Math.max(height(node.left), height(node.right));
}
function isBalanced(root) {
if (!root) return true;
let leftHeight = height(root.left);
let rightHeight = height(root.right);
if (Math.abs(leftHeight - rightHeight) > 1) return false;
return isBalanced(root.left) && isBalanced(root.right);
}
const root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.left.left = new Node(5);
console.log(isBalanced(root));
medium
A. false
B. true
C. undefined
D. Throws error
Solution
Step 1: Calculate heights of left and right subtrees
Left subtree height is 3 (nodes 2 -> 4 -> 5), right subtree height is 1 (node 3).
Step 2: Check balance condition at root
Height difference is 3 - 1 = 2, which is greater than 1, so tree is not balanced.