-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxDepthofBinaryTree.py
241 lines (193 loc) · 7.31 KB
/
MaxDepthofBinaryTree.py
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
104. Maximum Depth of Binary Tree
Easy
11.9K
195
Companies
Given the root of a binary tree, return its maximum depth.
A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: 3
Example 2:
Input: root = [1,null,2]
Output: 2
Constraints:
The number of nodes in the tree is in the range [0, 104].
-100 <= Node.val <= 100
#recursive solution (Python):
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
import math
def maxDepth(self, root: Optional[TreeNode]) -> int: #we are given the maxDepth method as part of the Solution class
if root is None: #base case of recursion
return 0
left = 1 + self.maxDepth(root.left) #if this is called, the program moves down to the left one node and then returns to line 41 to call the method from the top again. if we are on a leaf node, the recursion dosen't determine this until we return back to line 41 and 42 to determine if this is a base case.
right = 1 + self.maxDepth(root.right)
return max(left, right)
# when trying to determine the depth for the left child of D (which doesn't exist), the method call becomes self.maxDepth(None) because D.left is None.
# when a leaf node returns 0, it's just that particular next iteration returning 0 where self.maxDepth(None) not overriding the entire previous left historical height
# if you switch the order of lines 44 and 45, the result will still be correct
#refresher solution (10/25/23):
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
left = self.maxDepth(root.left)
right = self.maxDepth(root.right)
return max(left, right) + 1
#my solution 11/11/23:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
left = self.maxDepth(root.left) #these are the recursive calls
right = self.maxDepth(root.right) #these are the recursive calls
return max(left, right) + 1 #returning the bigger of the two + 1 because we are one above
#my solution 12/25/23 - good explanation of how recursion works:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0 #returns 0 back as the value of the maxDepth function back to the function call that started it after hitting bottom of tree aka base case
left = self.maxDepth(root.left) #finish getting to bottom of left node, so return 0 as the last function call, and we know how tall left subtree is, so move onto next line right = self.maxDepth(root.right) and do the same thing
right = self.maxDepth(root.right) #after we finish getting to the bottom of the right node, we return 0 from the base case, and we know how tall we are, so, in the next line, we add one to get to where we are, and then we pick the bigger of the two
return 1 + max(left, right)
#2/16/24:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
l = self.maxDepth(root.left)
r = self.maxDepth(root.right)
return 1 + max(l, r)
#3/5/24:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if root == None:
return 0
l = self.maxDepth(root.left)
r = self.maxDepth(root.right)
return 1 + max(l, r)
#3/26/24: (don't overthink - this is just traversing down both left and right and finding the bigger of the two!)
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
l = self.maxDepth(root.left)
r = self.maxDepth(root.right)
return 1 + max(l, r)
#5/10/24 refresher:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
l = self.maxDepth(root.left)
r = self.maxDepth(root.right)
return 1 + max(l, r)
#6/4/24 review:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
l = self.maxDepth(root.left)
r = self.maxDepth(root.right)
return 1 + max(l, r)
#7/7/24 review:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
l = self.maxDepth(root.left)
r = self.maxDepth(root.right)
return 1 + max(l, r)
#8/3/24 refresher:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
l = self.maxDepth(root.left)
r = self.maxDepth(root.right)
return 1 + max(l, r)
#9/7/24 review from grokking course:
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, data):
# self.data = data
# self.left = None
# self.right = None
from ds_v1.BinaryTree.BinaryTree import TreeNode
from collections import deque
def find_max_depth(root):
res = 0
def f(root, res):
if not root:
return 0
l = f(root.left, res)
r = f(root.right, res)
res = max(res, l + r)
return 1 + max(l, r)
return f(root, res)