-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathq0662.py
More file actions
29 lines (24 loc) · 1005 Bytes
/
q0662.py
File metadata and controls
29 lines (24 loc) · 1005 Bytes
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
#!/usr/bin/python3
from typing import List
# 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 widthOfBinaryTree_helper(self, root: TreeNode, length: int, position: int, positions: List[List[int]]):
if root:
if length > len(positions):
positions.append([])
positions[length - 1].append(position)
self.widthOfBinaryTree_helper(root.left, length + 1, position * 2, positions)
self.widthOfBinaryTree_helper(root.right, length + 1, position * 2 + 1, positions)
def widthOfBinaryTree(self, root: TreeNode) -> int:
result = 0
levels: List[List[int]] = []
self.widthOfBinaryTree_helper(root, 1, 0, levels)
for level in levels:
if result < level[-1] - level[0]:
result = level[-1] - level[0]
return result + 1