-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsameTree.py
More file actions
221 lines (169 loc) Β· 5.99 KB
/
sameTree.py
File metadata and controls
221 lines (169 loc) Β· 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#neetcode 25
class Solution(object):
def isSameTree(self, p, q):
# check if the both the trees are empty. If botha are empty it means they are equal
if not p and not q:
return True
# check if either p or q are empty and also check if the root values of p and q are equal
if not p or not q or p.val != q.val:
return False
# apply the above function to left and right nodes of p and q
result = self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
return result
#cWe're about to go *deep* into this. I'll explain it like I'm talking to someone who has *never* seen a line of code in their life.
# I'll break down every single line, show visual examples, and make sure you walk away feeling like an **absolute genius** when it comes to understanding this. π
# ## π¨ **What Is This Code Trying to Do?**
# The goal of this function is to **compare two trees** and check if they are **exactly the same**.
# ### π€ What Does "Same Tree" Mean?
# Two trees are the **same** if:
# 1. They have the **same shape** (structure).
# 2. They have the **same values** at every corresponding node.
# If *any* difference exists (structure or values), they are **not the same**.
# ---
# ## π **The Code**
# ```python
# class Solution(object):
# def isSameTree(self, p, q):
# # Check if both trees are empty
# if not p and not q:
# return True
# # Check if one is empty OR their values are different
# if not p or not q or p.val != q.val:
# return False
# # Recursively check left and right subtrees
# result = self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
# return result
# ```
# ---
# ## π³ **Visualizing the Trees**
# Let's take two trees:
# β
**Same Trees Example:**
# ```
# Tree 1 Tree 2
# 1 1
# / \ / \
# 2 3 2 3
# ```
# Both trees are **identical** in structure and values. So, the function should return `True`.
# β **Different Trees Example:**
# ```
# Tree 1 Tree 2
# 1 1
# / \ / \
# 2 3 2 4 β (Different value)
# ```
# Here, one value is different (`3` vs. `4`), so the function should return `False`.
# ---
# ## π οΈ **Breaking It Down Line-by-Line**
# ### **1οΈβ£ Class Definition**
# ```python
# class Solution(object):
# ```
# π‘ We're creating a **class** called `Solution`.
# A class is like a **blueprint** for writing reusable functions.
# ---
# ### **2οΈβ£ Defining the Function**
# ```python
# def isSameTree(self, p, q):
# ```
# π This function takes **two trees** (`p` and `q`) and checks if they are the same.
# ---
# ### **3οΈβ£ Checking if Both Trees Are Empty**
# ```python
# if not p and not q:
# return True
# ```
# π€ **What is happening here?**
# - `p` is the first tree.
# - `q` is the second tree.
# - `not p and not q` means **both trees are empty**.
# - If both are empty, they are **identical** (because *nothing* is identical to *nothing*).
# π‘ **Example:**
# ```
# Tree 1: None
# Tree 2: None
# ```
# π **Returns:** `True`
# ---
# ### **4οΈβ£ Checking If One Tree Is Empty or If Values Are Different**
# ```python
# if not p or not q or p.val != q.val:
# return False
# ```
# β **What does this mean?**
# We check **three** things here:
# 1. **One tree is empty, but the other isn't:**
# - Example:
# ```
# Tree 1: 1
# Tree 2: None
# ```
# π **Returns:** `False`
# 2. **Both trees exist, but their root values are different:**
# - Example:
# ```
# Tree 1: 1
# Tree 2: 2
# ```
# π **Returns:** `False`
# ---
# ### **5οΈβ£ Recursively Checking Left and Right Subtrees**
# ```python
# result = self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
# ```
# π‘ This is where **recursion** happens.
# 1. The function **calls itself** to check if the **left** sides of both trees are the same.
# 2. Then it **calls itself again** to check if the **right** sides are the same.
# 3. If **both** return `True`, we return `True`.
# 4. If **either** returns `False`, we return `False`.
# ---
# ### **6οΈβ£ Returning the Final Result**
# ```python
# return result
# ```
# - If **both left and right sides are the same**, return `True`.
# - Otherwise, return `False`.
# ---
# ## π **How the Function Works Step-by-Step**
# Let's take two trees and walk through each step of the function.
# ```
# Tree 1 Tree 2
# 1 1
# / \ / \
# 2 3 2 3
# ```
# ### **Step 1: Checking the Root**
# - `p = 1`, `q = 1`
# - They **match**, so we continue.
# ### **Step 2: Check Left Subtree (`p.left = 2`, `q.left = 2`)**
# - They **match**, so we go **deeper** into their children.
# - `p.left.left = None`, `q.left.left = None` β `True`
# - `p.left.right = None`, `q.left.right = None` β `True`
# β
Left side is **OK**.
# ### **Step 3: Check Right Subtree (`p.right = 3`, `q.right = 3`)**
# - They **match**, so we check their children.
# - `p.right.left = None`, `q.right.left = None` β `True`
# - `p.right.right = None`, `q.right.right = None` β `True`
# β
Right side is **OK**.
# ### **Final Answer:**
# - Left subtree is **same** (`True`).
# - Right subtree is **same** (`True`).
# - So, the function returns **`True`**. β
# ---
# ## π **Recap of Key Takeaways**
# 1. **We check if both trees are empty** β `True` if yes.
# 2. **If one tree is empty but the other isn't, they are not the same.**
# 3. **If root values are different, return `False`.**
# 4. **We check both left and right subtrees using recursion.**
# 5. **If both left and right are the same, the whole tree is the same!**
# ---
# ## βοΈ **Practice Time!**
# Try checking these trees:
# ```
# Tree 1 Tree 2
# 1 1
# / \ / \
# 2 3 2 4
# ```
# π What will the function return?
# Answer: **`False`**, because `3` β `4`.