📖 Scenario: Imagine you are building a system that manages a family tree. To keep the tree efficient for searching, you want to check if the tree is balanced. A balanced tree means the left and right branches of every node do not differ in height by more than one.
🎯 Goal: You will create a simple binary tree, write a method to check if it is balanced, and print the result.
📋 What You'll Learn
Create a binary tree with specific nodes
Add a helper method to calculate height
Write a method to check if the tree is balanced
Print the boolean result of the balance check
💡 Why This Matters
🌍 Real World
Balanced binary trees are used in databases and file systems to keep data organized for fast searching and updating.
💼 Career
Understanding tree balance is important for software engineers working on performance-critical applications and data structure optimization.
Progress0 / 4 steps
1
Create the Binary Tree Structure
Create a class called TreeNode with an integer val, and two TreeNode references called left and right. Then create a binary tree with root node value 1, left child value 2, and right child value 3. The left child of node 2 should be 4, and the right child of node 2 should be 5. The right child of node 3 should be 6.
DSA Java
Hint
Start by defining the TreeNode class with the required fields and constructor. Then create the nodes and link them as described.
2
Add a Helper Method to Calculate Height
Inside the Main class, add a static method called height that takes a TreeNode parameter called node and returns an int. This method should return 0 if node is null. Otherwise, it should return 1 + Math.max(height(node.left), height(node.right)).
DSA Java
Hint
The height of a node is 1 plus the maximum height of its left and right children. If the node is null, height is 0.
3
Write the Method to Check if the Tree is Balanced
Inside the Main class, add a static method called isBalanced that takes a TreeNode parameter called node and returns a boolean. This method should return true if node is null. Otherwise, it should check if the absolute difference between the height of node.left and node.right is less than or equal to 1, and recursively check if node.left and node.right are balanced. Return true only if all these conditions hold.
DSA Java
Hint
Check the height difference between left and right children. If difference is more than 1, return false. Otherwise, check recursively for left and right subtrees.
4
Print the Result of the Balance Check
In the main method, after creating the tree, add a line to print the result of calling isBalanced(root).
DSA Java
Hint
Use System.out.println(isBalanced(root)); to print the result.
Practice
(1/5)
1. What does it mean for a binary tree to be balanced?
easy
A. The height difference between left and right subtrees of every node is at most 1
B. All nodes have two children
C. The tree has the same number of nodes on left and right
D. The tree is a complete binary tree
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 C describe different 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 A
Quick Check:
Balance means height difference ≤ 1 [OK]
Hint: Balance means height difference ≤ 1 at every node [OK]
Common Mistakes:
Confusing balanced with complete tree
Thinking balance means equal number of nodes
Assuming all nodes must have two children
2. Which Java method signature correctly defines a function to check if a binary tree is balanced?
easy
A. boolean isBalanced(TreeNode root) void
B. public boolean isBalanced(TreeNode root)
C. public void isBalanced(TreeNode root) returns boolean
D. boolean isBalanced(TreeNode root) public
Solution
Step 1: Check Java method syntax
Correct Java method signature includes access modifier, return type, method name, and parameters: public boolean isBalanced(TreeNode root)
Step 2: Identify incorrect syntax
Options A, C, and D have invalid order or keywords not used in Java method declarations.
Final Answer:
public boolean isBalanced(TreeNode root) -> Option B