-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path662.py
52 lines (50 loc) · 1.74 KB
/
662.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
__________________________________________________________________________________________________
sample 28 ms submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def widthOfBinaryTree(self, root: TreeNode) -> int:
if not root:
return 0
curr_d=res =first= 0
curr = collections.deque([(root, 0, 0)])
while curr:
node, d, p = curr.popleft()
if d != curr_d:
first = p
curr_d = d
if node.left:
curr.append((node.left, d+1, 2*p))
if node.right:
curr.append((node.right, d+1, 2*p+1))
res = max(res, p-first+1)
return res
__________________________________________________________________________________________________
sample 13488 kb submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def widthOfBinaryTree(self, root: TreeNode) -> int:
# using BFS with virtual index for calculation
if not root: return 0
q = collections.deque()
q.append((root, 0))
res =0
while len(q):
nx = collections.deque()
res = max(res, q[-1][1]-q[0][1]+1)
while len(q):
cur, i = q.popleft()
if cur.left: nx.append((cur.left, 2*i+1))
if cur.right: nx.append((cur.right, 2*i+2))
q = nx
return res
__________________________________________________________________________________________________