-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_maximum_depth_of_binary_tree.py
53 lines (43 loc) · 1.48 KB
/
tree_maximum_depth_of_binary_tree.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
"""
link : https://leetcode.com/problems/maximum-depth-of-binary-tree/
문제: 이진 트리의 최대 깊이를 구하라.
1) DFS 풀이
2) BFS 풀이
- while 안에서 반복할 때, 현재 queue 의 길이만큼 끊어서 반복하면 같은 depth의 node들끼리 탐색가능하다. 반복하는 횟수만큼이 tree의 깊이이다.
"""
from collections import deque
from typing import Optional
# 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 maxDepthDfs(self, root: Optional[TreeNode]) -> int:
max_depth = 0
if not root:
return max_depth
def dfs(root, depth):
nonlocal max_depth
depth += 1
if depth > max_depth:
max_depth = depth
if root.left:
dfs(root.left, depth)
if root.right:
dfs(root.right, depth)
dfs(root, 0)
return max_depth
def maxDepthBfs(self, root: Optional[TreeNode]) -> int:
max_depth = 0
q = deque([root])
while q :
max_depth += 1
for _ in range(len(q)):
cur_root = q.popleft()
if cur_root.left:
q.append(cur_root.left)
if cur_root.right:
q.append(cur_root.right)
return max_depth