|
| 1 | +<h1 align='center'>Balanced - Tree - Check</h1> |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +**Problem URL :** [Balanced Tree Check](https://www.geeksforgeeks.org/problems/check-for-balanced-tree/1) |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +## Problem Explanation |
| 11 | + |
| 12 | +In this problem, we are tasked with checking whether a given binary tree is **balanced**. A binary tree is considered balanced if, for every node, the height difference between its left and right subtrees is at most 1. If any node in the tree violates this condition, the tree is unbalanced. |
| 13 | + |
| 14 | +#### Examples: |
| 15 | + |
| 16 | +1. **Example 1**: |
| 17 | + ``` |
| 18 | + 1 |
| 19 | + / \ |
| 20 | + 2 3 |
| 21 | + / |
| 22 | + 4 |
| 23 | + ``` |
| 24 | + - **Explanation**: This tree is **balanced**. The left subtree has height 2 (from node 1 to node 4), and the right subtree has height 1. The height difference is 1. |
| 25 | + |
| 26 | +2. **Example 2**: |
| 27 | + ``` |
| 28 | + 1 |
| 29 | + / |
| 30 | + 2 |
| 31 | + / |
| 32 | + 3 |
| 33 | + ``` |
| 34 | + - **Explanation**: This tree is **not balanced** because the left subtree has height 2 and the right subtree is empty (height 0). The height difference is 2. |
| 35 | + |
| 36 | +3. **Example 3**: |
| 37 | + - An empty tree (`root == NULL`) is considered **balanced**. |
| 38 | + |
| 39 | +### Step 2: Approach to Solve the Problem |
| 40 | + |
| 41 | +To determine if a binary tree is balanced, we can use a recursive approach: |
| 42 | + |
| 43 | +1. **Recursive Height Calculation**: |
| 44 | + - Create a helper function to compute the height of the tree while also checking the balance condition. |
| 45 | + - If at any point the balance condition is violated (the height difference is greater than 1), return a sentinel value (like `-1`) to indicate the tree is unbalanced. |
| 46 | + |
| 47 | +2. **Combine Height and Balance Check**: |
| 48 | + - For each node, calculate the heights of the left and right subtrees. |
| 49 | + - If both subtrees are balanced, return their maximum height plus one. |
| 50 | + - If not balanced, return `-1`. |
| 51 | + |
| 52 | +## Problem Solution |
| 53 | +```cpp |
| 54 | +class Solution{ |
| 55 | + public: |
| 56 | + int heights(Node* root){ |
| 57 | + if(root == NULL) return 0; |
| 58 | + |
| 59 | + int left = heights(root -> left); |
| 60 | + if(left == -1) return -1; |
| 61 | + |
| 62 | + int right = heights(root -> right); |
| 63 | + if(right == -1) return -1; |
| 64 | + |
| 65 | + if(abs(left - right) > 1) return -1; |
| 66 | + |
| 67 | + return max(left, right) + 1; |
| 68 | + } |
| 69 | + bool isBalanced(Node *root) |
| 70 | + { |
| 71 | + return heights(root) != -1; |
| 72 | + |
| 73 | + } |
| 74 | +}; |
| 75 | +``` |
| 76 | + |
| 77 | +## Problem Solution Explanation |
| 78 | + |
| 79 | +Here’s the code with detailed explanations: |
| 80 | + |
| 81 | +```cpp |
| 82 | +class Solution{ |
| 83 | +public: |
| 84 | + int heights(Node* root){ |
| 85 | + if(root == NULL) return 0; |
| 86 | +``` |
| 87 | +- **Explanation**: The base case checks if the current node is `NULL`. If it is, return `0`, indicating the height of an empty subtree. |
| 88 | +
|
| 89 | +```cpp |
| 90 | + int left = heights(root -> left); |
| 91 | + if(left == -1) return -1; |
| 92 | +``` |
| 93 | +- **Explanation**: |
| 94 | + - Recursively calculate the height of the left subtree. |
| 95 | + - If the left subtree is unbalanced (i.e., returns `-1`), return `-1` immediately. |
| 96 | + |
| 97 | +```cpp |
| 98 | + int right = heights(root -> right); |
| 99 | + if(right == -1) return -1; |
| 100 | +``` |
| 101 | +- **Explanation**: |
| 102 | + - Calculate the height of the right subtree. |
| 103 | + - If the right subtree is unbalanced (i.e., returns `-1`), return `-1` immediately. |
| 104 | + |
| 105 | +```cpp |
| 106 | + if(abs(left - right) > 1) return -1; |
| 107 | +``` |
| 108 | +- **Explanation**: |
| 109 | + - Check if the absolute difference between the heights of the left and right subtrees is greater than `1`. If so, return `-1`, indicating that the tree is unbalanced. |
| 110 | + |
| 111 | +```cpp |
| 112 | + return max(left, right) + 1; |
| 113 | + } |
| 114 | +``` |
| 115 | +- **Explanation**: |
| 116 | + - If the current node is balanced, return the height of the subtree, which is `1 + max(left, right)`. |
| 117 | + |
| 118 | +```cpp |
| 119 | + bool isBalanced(Node *root) |
| 120 | + { |
| 121 | + return heights(root) != -1; |
| 122 | + } |
| 123 | +}; |
| 124 | +``` |
| 125 | +- **Explanation**: |
| 126 | + - The `isBalanced` function calls the `heights` helper function and checks if it returns `-1`. If not, the tree is balanced; otherwise, it is unbalanced. |
| 127 | +
|
| 128 | +### Step-by-Step Example Walkthrough |
| 129 | +
|
| 130 | +Consider the tree: |
| 131 | +
|
| 132 | +``` |
| 133 | + 1 |
| 134 | + / \ |
| 135 | + 2 3 |
| 136 | + / |
| 137 | + 4 |
| 138 | +``` |
| 139 | +
|
| 140 | +1. **Node 4**: |
| 141 | + - Left and right children are `NULL`, so their heights are `0`. |
| 142 | + - Returns `height(4) = 1`. |
| 143 | + |
| 144 | +2. **Node 2**: |
| 145 | + - Left subtree height = `1` (from node `4`). |
| 146 | + - Right subtree height = `0` (no right child). |
| 147 | + - Height difference is `1`, so it returns `height(2) = 2`. |
| 148 | +
|
| 149 | +3. **Node 3**: |
| 150 | + - Left and right children are `NULL`, so height = `1`. |
| 151 | +
|
| 152 | +4. **Node 1**: |
| 153 | + - Left subtree height = `2` (from node `2`). |
| 154 | + - Right subtree height = `1` (from node `3`). |
| 155 | + - Height difference is `1`, so it returns `height(1) = 3`. |
| 156 | +
|
| 157 | +The function returns `true`, indicating the tree is balanced. |
| 158 | +
|
| 159 | +### Step 4: Time and Space Complexity |
| 160 | +
|
| 161 | +1. **Time Complexity**: **O(N)**, where `N` is the number of nodes in the tree. |
| 162 | + - Each node is visited once to calculate the height, ensuring efficiency. |
| 163 | +
|
| 164 | +2. **Space Complexity**: **O(H)**, where `H` is the height of the tree. |
| 165 | + - The space complexity is determined by the maximum height of the recursion stack. In the worst case (skewed tree), it could be `O(N)`, while for a balanced tree, it is `O(log N)`. |
0 commit comments