Skip to content

Commit 5cb3d98

Browse files
committed
update the maxdepth using pq
1 parent a7da2bd commit 5cb3d98

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed
Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
class Solution:
22

33
def maxDepth(self, root: Optional[TreeNode]) -> int:
4-
54
if not root:
65
return 0
6+
queue = deque([root])
7+
print(queue)
8+
depth = 0
9+
10+
while queue:
11+
depth += 1
12+
print(len(queue))
13+
for _ in range(len(queue)):
14+
node = queue.popleft()
15+
if node.left:
16+
queue.append(node.left)
17+
if node.right:
18+
queue.append(node.right)
19+
return depth
20+
21+
22+
# if not root:
23+
# return 0
724

8-
left_depth = self.maxDepth(root.left)
9-
right_depth = self.maxDepth(root.right)
25+
# left_depth = self.maxDepth(root.left)
26+
# right_depth = self.maxDepth(root.right)
1027

11-
return max(left_depth, right_depth) + 1
28+
# return max(left_depth, right_depth) + 1

0 commit comments

Comments
 (0)