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