Check if Binary Tree is Balanced
📖 Scenario: You are working on a program that manages a binary tree structure. A balanced binary tree is one where the difference in height between the left and right subtrees of every node is at most 1. This helps keep operations efficient.Imagine you want to check if your tree is balanced to ensure fast searching and insertion.
🎯 Goal: Build a TypeScript program that creates a binary tree, sets up a helper function to check balance, implements the logic to verify if the tree is balanced, and finally prints the result.
📋 What You'll Learn
Create a binary tree node class called
TreeNode with val, left, and right propertiesCreate a sample binary tree with exact nodes and structure
Create a helper function called
checkHeight that returns the height of a subtree or -1 if unbalancedCreate a function called
isBalanced that uses checkHeight to determine if the tree is balancedPrint
true or false depending on whether the tree is balanced💡 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