|
| 1 | +<h1 align='center'>Height - of - Binary - Tree</h1> |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +**Problem URL :** [Height of Binary Tree](https://www.geeksforgeeks.org/problems/height-of-binary-tree/1?itm_source=geeksforgeeks&itm_medium=article&itm_campaign=practice_card) |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +## Problem Explanation |
| 11 | + |
| 12 | +The problem, "Height of Binary Tree," is about determining how tall a binary tree is. The **height of a binary tree** is defined as the number of edges in the longest path from the root node (top of the tree) to a leaf node (a node with no children). |
| 13 | + |
| 14 | +If there are `N` levels in the tree, the height is `N`. In an empty tree (no nodes), the height is `0`. |
| 15 | + |
| 16 | +### Examples |
| 17 | + |
| 18 | +1. **Example 1**: A tree with multiple levels: |
| 19 | + |
| 20 | + ``` |
| 21 | + 1 |
| 22 | + / \ |
| 23 | + 2 3 |
| 24 | + / \ |
| 25 | + 4 5 |
| 26 | + ``` |
| 27 | + - Here, the longest path from the root (1) to the deepest leaf (either 4 or 5) has **two edges** (1 → 2 → 4 or 1 → 2 → 5). |
| 28 | + - So, the height of this tree is `3`. |
| 29 | + |
| 30 | +2. **Example 2**: A single-node tree: |
| 31 | + |
| 32 | + ``` |
| 33 | + 1 |
| 34 | + ``` |
| 35 | + - With only one node, there’s no edge from the root to a leaf, so the height is `1`. |
| 36 | + |
| 37 | +3. **Example 3**: An empty tree: |
| 38 | + - An empty tree has no nodes, so the height is `0`. |
| 39 | + |
| 40 | +### Approach to Solving the Problem (Step-by-Step) |
| 41 | + |
| 42 | +To solve this problem, let’s approach it logically: |
| 43 | + |
| 44 | +1. **Understanding the Tree’s Structure**: |
| 45 | + - For each node in the tree, the height of the tree at that node is determined by the maximum height of its left and right subtrees plus one (for the node itself). |
| 46 | + |
| 47 | +2. **Breaking Down the Problem**: |
| 48 | + - The height of an empty node (base case) is `0`. |
| 49 | + - For each node with children, we need to: |
| 50 | + - Recursively determine the height of its left subtree. |
| 51 | + - Recursively determine the height of its right subtree. |
| 52 | + - Use the maximum of these two heights and add `1` to count the current node's height. |
| 53 | + |
| 54 | +3. **Using Recursion**: |
| 55 | + - We can solve this problem efficiently using recursion by visiting each node once and calculating the height of its subtrees. |
| 56 | + |
| 57 | +4. **Steps to Think as a Beginner**: |
| 58 | + - Think of the root as the base node. |
| 59 | + - Calculate the height of each subtree starting from the root and working your way down to the leaf nodes. |
| 60 | + - At each node, the height is `1 + max(height of left subtree, height of right subtree)`. |
| 61 | + |
| 62 | +## Problem Solution |
| 63 | +```cpp |
| 64 | +class Solution{ |
| 65 | + public: |
| 66 | + //Function to find the height of a binary tree. |
| 67 | + int height(struct Node* node){ |
| 68 | + // base case |
| 69 | + if(node == NULL) return 0; |
| 70 | + |
| 71 | + int left = height(node -> left); |
| 72 | + int right = height(node -> right); |
| 73 | + |
| 74 | + return max(left, right) + 1; |
| 75 | + } |
| 76 | +}; |
| 77 | +``` |
| 78 | + |
| 79 | +## Problem Solution Explanation |
| 80 | + |
| 81 | +1. **Base Case**: |
| 82 | + ```cpp |
| 83 | + if(node == NULL) return 0; |
| 84 | + ``` |
| 85 | + - If the `node` is `NULL` (meaning it's an empty branch or an empty tree), the function returns `0` because there are no nodes to contribute to the height. |
| 86 | + |
| 87 | +2. **Recursive Calls for Left and Right Subtrees**: |
| 88 | + ```cpp |
| 89 | + int left = height(node->left); |
| 90 | + int right = height(node->right); |
| 91 | + ``` |
| 92 | + - The function recursively calls itself on `node->left` and `node->right` to calculate the height of each subtree. |
| 93 | + - Each call goes deeper into the tree until it reaches a `NULL` node, at which point it returns `0` and unwinds the recursion. |
| 94 | + |
| 95 | +3. **Calculating Height of Current Node**: |
| 96 | + ```cpp |
| 97 | + return max(left, right) + 1; |
| 98 | + ``` |
| 99 | + - `max(left, right)` finds the height of the taller subtree. |
| 100 | + - Adding `1` accounts for the height of the current node itself. |
| 101 | + - This value is returned up the recursive call stack, building the height of each node layer by layer until reaching the root. |
| 102 | + |
| 103 | +#### Example Walkthrough |
| 104 | + |
| 105 | +For the tree: |
| 106 | + |
| 107 | +``` |
| 108 | + 1 |
| 109 | + / \ |
| 110 | + 2 3 |
| 111 | + / |
| 112 | + 4 |
| 113 | +``` |
| 114 | + |
| 115 | +1. Starting at the root (node 1), `height(node->left)` is calculated by moving to node 2. |
| 116 | +2. For node 2, `height(node->left)` is calculated by moving to node 4. |
| 117 | +3. Node 4 has no children, so `height(node->left)` and `height(node->right)` return `0`, making `height(4) = 1`. |
| 118 | +4. Moving back to node 2, `height(node->right)` returns `0` because node 2 has no right child. |
| 119 | +5. Now, `height(2) = max(1, 0) + 1 = 2`. |
| 120 | +6. Moving back to the root (node 1), `height(node->right)` is calculated for node 3, which returns `1` since node 3 has no children. |
| 121 | +7. Finally, `height(1) = max(2, 1) + 1 = 3`, which is the height of the entire tree. |
| 122 | + |
| 123 | +### Time and Space Complexity |
| 124 | + |
| 125 | +1. **Time Complexity**: |
| 126 | + - The time complexity is **O(N)**, where `N` is the number of nodes in the tree. |
| 127 | + - This is because the function visits each node exactly once to calculate its height. |
| 128 | + |
| 129 | +2. **Space Complexity**: |
| 130 | + - The space complexity is **O(H)**, where `H` is the height of the tree. |
| 131 | + - This complexity comes from the recursion stack. In the worst case (for a skewed tree), the height could be `N`, so the space complexity would be `O(N)`. |
| 132 | + - For a balanced tree, the height would be closer to `log(N)`, making the space complexity `O(log(N))`. |
| 133 | + |
| 134 | +This approach is efficient for finding the height of a binary tree because it calculates the height of each subtree once and only requires memory for the current path in the recursive stack. |
0 commit comments