|
| 1 | +<h1 align='center'>Identical - Trees</h1> |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +**Problem URL :** [Identical Trees](https://www.geeksforgeeks.org/problems/determine-if-two-trees-are-identical/1) |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +## Problem Explanation |
| 11 | + |
| 12 | +The problem requires checking whether two binary trees are identical. Two trees are considered identical if they have the same structure and node values at each corresponding position. |
| 13 | + |
| 14 | +#### Examples: |
| 15 | + |
| 16 | +1. **Example 1**: |
| 17 | + - **Tree 1**: |
| 18 | + ``` |
| 19 | + 1 |
| 20 | + / \ |
| 21 | + 2 3 |
| 22 | + ``` |
| 23 | + - **Tree 2**: |
| 24 | + ``` |
| 25 | + 1 |
| 26 | + / \ |
| 27 | + 2 3 |
| 28 | + ``` |
| 29 | + - **Output**: `True` (The trees are identical) |
| 30 | + |
| 31 | +2. **Example 2**: |
| 32 | + - **Tree 1**: |
| 33 | + ``` |
| 34 | + 1 |
| 35 | + / \ |
| 36 | + 2 3 |
| 37 | + ``` |
| 38 | + - **Tree 2**: |
| 39 | + ``` |
| 40 | + 1 |
| 41 | + / \ |
| 42 | + 2 4 |
| 43 | + ``` |
| 44 | + - **Output**: `False` (The trees are not identical because the values of the right children differ) |
| 45 | + |
| 46 | +3. **Example 3**: |
| 47 | + - **Tree 1**: |
| 48 | + ``` |
| 49 | + 1 |
| 50 | + / \ |
| 51 | + 2 3 |
| 52 | + ``` |
| 53 | + - **Tree 2**: |
| 54 | + ``` |
| 55 | + 1 |
| 56 | + / |
| 57 | + 2 |
| 58 | + ``` |
| 59 | + - **Output**: `False` (The structure of the trees is different) |
| 60 | + |
| 61 | +### Step 2: Approach to Solve the Problem |
| 62 | + |
| 63 | +To determine if two binary trees are identical, we can use a recursive approach: |
| 64 | + |
| 65 | +1. **Base Case**: |
| 66 | + - If both nodes are `NULL`, they are identical. |
| 67 | + - If one node is `NULL` and the other is not, they are not identical. |
| 68 | + |
| 69 | +2. **Value Comparison**: |
| 70 | + - Compare the values of the current nodes of both trees. If they differ, the trees are not identical. |
| 71 | + |
| 72 | +3. **Recursive Comparison**: |
| 73 | + - Recursively check the left subtrees and right subtrees of both trees. |
| 74 | + - If both the left and right subtree checks return `true`, the trees are identical. |
| 75 | + |
| 76 | +## Problem Solution |
| 77 | +```cpp |
| 78 | +class Solution |
| 79 | +{ |
| 80 | + public: |
| 81 | + //Function to check if two trees are identical. |
| 82 | + bool isIdentical(Node *r1, Node *r2) |
| 83 | + { |
| 84 | + |
| 85 | + if(r1 == NULL && r2 == NULL) return true; |
| 86 | + if(r1 == NULL && r2 != NULL) return false; |
| 87 | + if(r1 != NULL && r2 == NULL) return false; |
| 88 | + |
| 89 | + bool left = isIdentical(r1 -> left, r2 -> left); |
| 90 | + bool right = isIdentical(r1 -> right, r2 -> right); |
| 91 | + |
| 92 | + bool value = r1 -> data == r2 -> data; |
| 93 | + |
| 94 | + if(left && right && value) return true; |
| 95 | + |
| 96 | + return false; |
| 97 | + } |
| 98 | +}; |
| 99 | +``` |
| 100 | + |
| 101 | +## Problem Solution Explanation |
| 102 | +Here’s the code with detailed explanations: |
| 103 | + |
| 104 | +```cpp |
| 105 | +class Solution |
| 106 | +{ |
| 107 | + public: |
| 108 | + //Function to check if two trees are identical. |
| 109 | + bool isIdentical(Node *r1, Node *r2) |
| 110 | + { |
| 111 | +``` |
| 112 | +- **Explanation**: This begins the definition of the `isIdentical` function, which takes two nodes (`r1` and `r2`) as input. |
| 113 | +
|
| 114 | +```cpp |
| 115 | + if(r1 == NULL && r2 == NULL) return true; |
| 116 | +``` |
| 117 | +- **Explanation**: If both nodes are `NULL`, it means we have reached the end of both trees simultaneously, so they are identical up to this point. Return `true`. |
| 118 | + |
| 119 | +```cpp |
| 120 | + if(r1 == NULL && r2 != NULL) return false; |
| 121 | + if(r1 != NULL && r2 == NULL) return false; |
| 122 | +``` |
| 123 | +- **Explanation**: |
| 124 | + - If one node is `NULL` and the other is not, the trees cannot be identical. Hence, return `false` for both conditions. |
| 125 | + |
| 126 | +```cpp |
| 127 | + bool left = isIdentical(r1 -> left, r2 -> left); |
| 128 | + bool right = isIdentical(r1 -> right, r2 -> right); |
| 129 | +``` |
| 130 | +- **Explanation**: |
| 131 | + - Recursively check the left children of both trees and store the result in `left`. |
| 132 | + - Recursively check the right children of both trees and store the result in `right`. |
| 133 | + |
| 134 | +```cpp |
| 135 | + bool value = r1 -> data == r2 -> data; |
| 136 | +``` |
| 137 | +- **Explanation**: Compare the values of the current nodes. This checks if the data stored in `r1` and `r2` are the same. |
| 138 | + |
| 139 | +```cpp |
| 140 | + if(left && right && value) return true; |
| 141 | +``` |
| 142 | +- **Explanation**: If both the left subtree and right subtree are identical, and the current nodes' values are the same, return `true`. |
| 143 | + |
| 144 | +```cpp |
| 145 | + return false; |
| 146 | + } |
| 147 | +}; |
| 148 | +``` |
| 149 | +- **Explanation**: If any of the above conditions fail, return `false`, indicating the trees are not identical. |
| 150 | + |
| 151 | +### Step-by-Step Example Walkthrough |
| 152 | + |
| 153 | +Consider the following trees: |
| 154 | + |
| 155 | +- **Tree 1**: |
| 156 | +``` |
| 157 | + 1 |
| 158 | + / \ |
| 159 | + 2 3 |
| 160 | +``` |
| 161 | + |
| 162 | +- **Tree 2**: |
| 163 | +``` |
| 164 | + 1 |
| 165 | + / \ |
| 166 | + 2 3 |
| 167 | +``` |
| 168 | + |
| 169 | +1. **Compare Node 1**: |
| 170 | + - Both values are `1`, proceed to compare left and right subtrees. |
| 171 | + |
| 172 | +2. **Compare Node 2 (Left Subtree)**: |
| 173 | + - Both are `2`, proceed to compare their children (both `NULL`). |
| 174 | + |
| 175 | +3. **Compare Node 3 (Right Subtree)**: |
| 176 | + - Both are `3`, proceed to compare their children (both `NULL`). |
| 177 | + |
| 178 | +4. All comparisons return `true`, thus the function returns `true` for the trees being identical. |
| 179 | + |
| 180 | +### Step 4: Time and Space Complexity |
| 181 | + |
| 182 | +1. **Time Complexity**: **O(N)**, where `N` is the number of nodes in the trees. |
| 183 | + - We traverse each node in both trees exactly once to check for equality. |
| 184 | + |
| 185 | +2. **Space Complexity**: **O(H)**, where `H` is the height of the trees. |
| 186 | + - The space complexity is due to the recursive function calls on the stack. In the worst case (skewed tree), it could be `O(N)`, while for balanced trees, it would be `O(log N)`. |
| 187 | + |
| 188 | +Overall, this approach effectively checks for the structural and value equivalence of two binary trees using recursion. |
0 commit comments