-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0662-maximum-width-of-binary-tree.py
More file actions
40 lines (34 loc) · 1.07 KB
/
0662-maximum-width-of-binary-tree.py
File metadata and controls
40 lines (34 loc) · 1.07 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
# time complexity: O(n)
# space complexity: O(n)
from collections import deque
from typing import List, Optional
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def widthOfBinaryTree(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
maxWidth = 0
queue = deque()
queue.append((root, 0))
while queue:
queLen = len(queue)
_, headIdx = queue[0]
for _ in range(queLen):
node, colIdx = queue.popleft()
if node.left:
queue.append((node.left, colIdx * 2))
if node.right:
queue.append((node.right, colIdx * 2 + 1))
maxWidth = max(maxWidth, colIdx - headIdx + 1)
return maxWidth
root = TreeNode(1)
root.left = TreeNode(3)
root.right = TreeNode(2)
root.left.left = TreeNode(5)
root.left.right = TreeNode(3)
root.right.right = TreeNode(9)
print(Solution().widthOfBinaryTree(root))