Skip to content

Commit 35d7816

Browse files
Solve : Maximum Depth of Binary Tree - DFS
1 parent a604816 commit 35d7816

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed
Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
from collections import deque
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
27

38
class Solution:
4-
def maxDepth(self, root):
9+
def maxDepth(self, root: Optional[TreeNode]) -> int:
510
if not root:
611
return 0
7-
8-
queue = deque([root])
9-
depth = 0
10-
11-
while queue:
12-
for _ in range(len(queue)):
13-
node = queue.popleft()
14-
if node.left:
15-
queue.append(node.left)
16-
if node.right:
17-
queue.append(node.right)
18-
depth += 1
19-
20-
return depth
12+
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))

0 commit comments

Comments
 (0)