锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
2 #Runtime: 998 ms (Beats 89.58%)
3 #Memory: 23.6 MB (Beats 98.27%)
4
5 class Solution(object):
6 def dailyTemperatures(self, temperatures):
7 """
8 :type temperatures: List[int]
9 :rtype: List[int]
10 """
11 ans = [0] * len(temperatures)
12 stk = []
13 for i, t in enumerate(temperatures):
14 if not stk:
15 stk.append(i)
16 else:
17 while stk and temperatures[stk[-1]] < t:
18 idx = stk.pop()
19 ans[idx] = i - idx
20 stk.append(i)
21 return ans
]]>
2 #Runtime: 911 ms (Beats 96.43%)
3 #Memory: 136.7 MB (Beats 10.71%)
4
5 # Definition for a binary tree node.
6 # class TreeNode(object):
7 # def __init__(self, val=0, left=None, right=None):
8 # self.val = val
9 # self.left = left
10 # self.right = right
11 class Solution(object):
12 def pseudoPalindromicPaths (self, root, cnt = 0):
13 """
14 :type root: TreeNode
15 :rtype: int
16 """
17 if not root:
18 return 0
19 cnt ^= 1 << (root.val - 1)
20 if root.left is None and root.right is None:
21 return 1 if cnt & (cnt - 1) == 0 else 0
22 return self.pseudoPalindromicPaths(root.left, cnt) + self.pseudoPalindromicPaths(root.right, cnt)
23
]]>
2 #Runtime: 44 ms (Beats 7.3%)
3 #Memory: 16.2 MB (Beats 35.94%)
4
5 # Definition for a binary tree node.
6 # class TreeNode(object):
7 # def __init__(self, val=0, left=None, right=None):
8 # self.val = val
9 # self.left = left
10 # self.right = right
11 class Solution(object):
12 def tree2str(self, root):
13 """
14 :type root: TreeNode
15 :rtype: str
16 """
17 self.ans = ""
18 def DFS(node):
19 self.ans += str(node.val)
20 if node.left or node.right:
21 self.ans += '('
22 if node.left:
23 DFS(node.left)
24 self.ans += ')'
25 if node.right:
26 self.ans += '('
27 DFS(node.right)
28 self.ans += ')'
29
30 if root:
31 DFS(root)
32 return self.ans
]]>
鐢╬ython heapq鍋氭ā鎷?br />
2 #Runtime: 1029 ms (Beats 20%)
3 #Memory: 44.4 MB (Beats 25.71%)
4
5 class SeatManager(object):
6
7 def __init__(self, n):
8 """
9 :type n: int
10 """
11 self.seats = []
12 self.n = n
13 for i in xrange(1, n + 1):
14 heapq.heappush(self.seats, i)
15
16
17 def reserve(self):
18 """
19 :rtype: int
20 """
21 if self.seats > 0:
22 seat = heapq.heappop(self.seats)
23 return seat
24 return -1
25
26
27 def unreserve(self, seatNumber):
28 """
29 :type seatNumber: int
30 :rtype: None
31 """
32 heapq.heappush(self.seats, seatNumber)
33
34
35
36
37 # Your SeatManager object will be instantiated and called as such:
38 # obj = SeatManager(n)
39 # param_1 = obj.reserve()
40 # obj.unreserve(seatNumber)
]]>
2 #Runtime: 26 ms (Beats 68.6%)
3 #Memory: 17.7 MB (Beats 71.73%)
4
5 # Definition for a binary tree node.
6 # class TreeNode(object):
7 # def __init__(self, val=0, left=None, right=None):
8 # self.val = val
9 # self.left = left
10 # self.right = right
11 class Solution(object):
12 def largestValues(self, root):
13 """
14 :type root: TreeNode
15 :rtype: List[int]
16 """
17 if not root:
18 return []
19 q = deque([root])
20 ans = []
21 while q:
22 sz = len(q)
23 t = float('-inf')
24 while sz:
25 sz -= 1
26 node = q.popleft()
27 t = max(t, node.val)
28 if node.left:
29 q.append(node.left)
30 if node.right:
31 q.append(node.right)
32 ans.append(t)
33 return ans
]]>
2 #Runtime: 18 ms (Beats 31.98%)
3 #Memory: 13.3 MB (Beats 27.21%)
4
5 class Solution(object):
6 def backspaceCompare(self, s, t):
7 """
8 :type s: str
9 :type t: str
10 :rtype: bool
11 """
12 stk1 = []
13 stk2 = []
14 for ch in s:
15 if ch == '#':
16 if len(stk1):
17 stk1.pop()
18 else:
19 stk1.append(ch)
20 for ch in t:
21 if ch == '#':
22 if len(stk2):
23 stk2.pop()
24 else:
25 stk2.append(ch)
26 return stk1 == stk2
]]>
2 #Runtime: 224 ms (Beats 92.11%)
3 #Memory: 15.5 MB (Beats 71.5%錛?/span>
4
5 class Solution(object):
6 def validateBinaryTreeNodes(self, n, leftChild, rightChild):
7 """
8 :type n: int
9 :type leftChild: List[int]
10 :type rightChild: List[int]
11 :rtype: bool
12 """
13 rt = 0
14 child_nodes = set(leftChild + rightChild)
15 for i in xrange(n):
16 if i not in child_nodes:
17 rt = i
18 vis = set()
19 q = deque([rt])
20 while q:
21 node = q.popleft()
22 if node in vis:
23 return False
24 vis.add(node)
25 if leftChild[node] != -1:
26 q.append(leftChild[node])
27 if rightChild[node] != -1:
28 q.append(rightChild[node])
29 return len(vis) == n
]]>
2 #Runtime: 473 ms (Beats 70.43%)
3 #Memory: 25 MB (Beats 91.14%)
4
5 class Solution(object):
6 def findDuplicate(self, nums):
7 """
8 :type nums: List[int]
9 :rtype: int
10 """
11 slow, fast, ans = 0, 0, 0
12 while True:
13 slow = nums[slow]
14 fast = nums[nums[fast]]
15 if slow == fast:
16 while ans != slow:
17 ans = nums[ans]
18 slow = nums[slow]
19 return ans
]]>
2 #Runtime: 15 ms (Beats 63.77%)
3 #Memory: 13.7 MB (Beats 14.88%)
4
5 # Definition for singly-linked list.
6 # class ListNode(object):
7 # def __init__(self, val=0, next=None):
8 # self.val = val
9 # self.next = next
10 class Solution(object):
11 def reverseBetween(self, head, left, right):
12 """
13 :type head: ListNode
14 :type left: int
15 :type right: int
16 :rtype: ListNode
17 """
18 ex_head = ListNode()
19 ex_head.next = head
20 pre = ex_head
21 for _ in range(left - 1):
22 pre = pre.next
23 cur = pre.next
24 tp = None
25 for _ in range(right - left + 1):
26 nxt = cur.next
27 cur.next = tp
28 tp = cur
29 cur = nxt
30 pre.next.next = cur
31 pre.next = tp
32 return ex_head.next
]]>
2 #Runtime: 15 ms (Beats 93.44%)
3 #Memory: 13.6 MB (Beats 73.77%)
4
5 # Definition for singly-linked list.
6 # class ListNode(object):
7 # def __init__(self, val=0, next=None):
8 # self.val = val
9 # self.next = next
10 class Solution(object):
11 def splitListToParts(self, head, k):
12 """
13 :type head: ListNode
14 :type k: int
15 :rtype: List[ListNode]
16 """
17 n = 0
18 p = head
19 pre = None
20 while p:
21 n += 1
22 p = p.next
23 l, t = n//k, n%k
24 p = head
25 ans = []
26 for i in range(k):
27 ans.append(p)
28 for _ in range(l):
29 if p:
30 pre = p
31 p = p.next
32 if t and p:
33 pre = p
34 p = p.next
35 t -= 1
36 if pre:
37 pre.next = None
38 return ans
]]>
2 #Runtime: 28 ms (Beats 92.87%)
3 #Memory: 13.9 MB (Beats 70.54%)
4
5 """
6 # Definition for a Node.
7 class Node:
8 def __init__(self, x, next=None, random=None):
9 self.val = int(x)
10 self.next = next
11 self.random = random
12 """
13
14 class Solution(object):
15 def copyRandomList(self, head):
16 """
17 :type head: Node
18 :rtype: Node
19 """
20 dic = {None:None}
21 cur = head
22 while cur:
23 dic[cur] = Node(cur.val)
24 cur = cur.next
25 cur = head
26 while cur:
27 cp = dic[cur]
28 cp.next = dic[cur.next]
29 cp.random = dic[cur.random]
30 cur = cur.next
31 return dic[head]
]]>
python鐗?br />
2 #Runtime: 57 ms (Beats 75.48%)
3 #Memory: 20.6 MB (Beats 48.63%)
4
5 # Definition for singly-linked list.
6 # class ListNode:
7 # def __init__(self, x):
8 # self.val = x
9 # self.next = None
10
11 class Solution:
12 def hasCycle(self, head: Optional[ListNode]) -> bool:
13 slow, fast = head, head
14 while slow and fast and fast.next:
15 fast = fast.next.next
16 slow = slow.next
17 if slow == fast:
18 return True
19 return False
C++鐗?br />
2 //Runtime: 80 ms (Beats 6.2%)
3 //Memory: N/A (Beats N/A)
4
5 /**
6 * Definition for singly-linked list.
7 * struct ListNode {
8 * int val;
9 * ListNode *next;
10 * ListNode(int x) : val(x), next(NULL) {}
11 * };
12 */
13 class Solution {
14 public:
15 bool hasCycle(ListNode *head) {
16 ListNode *slow = head, *fast = head;
17 while(fast && fast->next) {
18 slow = slow->next;
19 fast = fast->next->next;
20 if(slow == fast) break;
21 }
22 if(fast == NULL || fast->next == NULL) return false;
23 return true;
24 }
25 };
]]>
2 #Runtime: 7 ms (Beats 97.10%)
3 #Memory: 13.3 MB (Beats 55.58%)
4
5 class MyStack(object):
6
7 def __init__(self):
8 self.que = deque()
9
10 def push(self, x):
11 """
12 :type x: int
13 :rtype: None
14 """
15 self.que.append(x)
16
17 def pop(self):
18 """
19 :rtype: int
20 """
21 for _ in range(len(self.que) - 1):
22 self.que.append(self.que.popleft())
23 return self.que.popleft()
24
25
26 def top(self):
27 """
28 :rtype: int
29 """
30 return self.que[-1]
31
32
33 def empty(self):
34 """
35 :rtype: bool
36 """
37 return len(self.que) == 0
38
39
40
41 # Your MyStack object will be instantiated and called as such:
42 # obj = MyStack()
43 # obj.push(x)
44 # param_2 = obj.pop()
45 # param_3 = obj.top()
46 # param_4 = obj.empty()
]]>
2 #Runtime: 1773 ms (Beats 16%)
3 #Memory: 30.5 MB (Beats 48.38%)
4
5 class Solution(object):
6 def maxSlidingWindow(self, nums, k):
7 """
8 :type nums: List[int]
9 :type k: int
10 :rtype: List[int]
11 """
12 wd_max = []
13 ans = []
14 for i in range(len(nums)):
15 if wd_max and wd_max[0] == i - k:
16 wd_max.pop(0)
17 while wd_max and nums[wd_max[-1]] < nums[i]:
18 wd_max.pop()
19 wd_max.append(i)
20 if i >= k - 1:
21 ans.append(nums[wd_max[0]])
22 return ans
]]>
2 #Runtime: 822 ms (Beats 46.88%)
3 #Memory: 23.3 MB (Beats 81.50%)
4
5 class Solution(object):
6 def findKthLargest(self, nums, k):
7 """
8 :type nums: List[int]
9 :type k: int
10 :rtype: int
11 """
12 hp = []
13 for n in nums:
14 heapq.heappush(hp, n)
15 if len(hp) > k:
16 heapq.heappop(hp)
17 return hp[0]
]]>
C++
2 //Runtime: 128 ms (Beats 7.1%)
3
4 /**
5 * Definition for binary tree
6 * struct TreeNode {
7 * int val;
8 * TreeNode *left;
9 * TreeNode *right;
10 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
11 * };
12 */
13 class Solution {
14 public:
15 vector<TreeNode *> sov(int l, int r) {
16 vector<TreeNode *> ans;
17 if(l > r) ans.push_back(NULL);
18 else {
19 for(int i = l; i <= r; ++i) {
20 vector<TreeNode *> left = sov(l, i - 1);
21 vector<TreeNode *> right = sov(i + 1, r);
22 for(int ll = 0; ll < left.size(); ++ll) {
23 for(int rr = 0; rr < right.size(); ++rr) {
24 TreeNode *root = new TreeNode(i);
25 root->left = left[ll];
26 root->right = right[rr];
27 ans.push_back(root);
28 }
29 }
30 }
31 }
32 return ans;
33 }
34
35 vector<TreeNode *> generateTrees(int n) {
36 return sov(1, n);
37 }
38 };
Python
2 #Runtime: 38 ms (Beats 79.12%)
3 #Memory: 16.2 MB (Beats 63.19%)
4
5 # Definition for a binary tree node.
6 # class TreeNode(object):
7 # def __init__(self, val=0, left=None, right=None):
8 # self.val = val
9 # self.left = left
10 # self.right = right
11 class Solution(object):
12 def generateTrees(self, n):
13 """
14 :type n: int
15 :rtype: List[TreeNode]
16 """
17
18 def dp(l, r):
19 if l > r:
20 return [None]
21 ans = []
22 for i in range(l, r + 1):
23 l_tree = dp(l, i - 1)
24 r_tree = dp(i + 1, r)
25 for ll in l_tree:
26 for rr in r_tree:
27 root = TreeNode(i)
28 root.left = ll
29 root.right = rr
30 ans.append(root)
31 return ans
32 return dp(1, n)
]]>
2 #Runtime: 98 ms (Beats 14.98%)
3 #Memory: 14.6 MB (Beats 16.21%)
4
5 class Solution(object):
6 def asteroidCollision(self, asteroids):
7 """
8 :type asteroids: List[int]
9 :rtype: List[int]
10 """
11 stk = [asteroids[0]]
12 for x in asteroids[1:]:
13 stk.append(x)
14 while len(stk) > 1 and stk[-2] > 0 and stk[-1] < 0:
15 pre1 = stk[-1]
16 pre2 = stk[-2]
17 stk.pop()
18 stk.pop()
19 if abs(pre1) != abs(pre2):
20 stk.append(pre1) if abs(pre1) > abs(pre2) else stk.append(pre2)
21 return stk
]]>
2 #Runtime: 56 ms (Beats 64.47%)
3 #Memory: 13.6 MB (Beats 13.21%)
4
5 # Definition for singly-linked list.
6 # class ListNode(object):
7 # def __init__(self, val=0, next=None):
8 # self.val = val
9 # self.next = next
10 class Solution(object):
11 def addTwoNumbers(self, l1, l2):
12 """
13 :type l1: ListNode
14 :type l2: ListNode
15 :rtype: ListNode
16 """
17 n1, n2 = [], []
18 while l1:
19 n1.append(l1.val)
20 l1 = l1.next
21 while l2:
22 n2.append(l2.val)
23 l2 = l2.next
24 t = 0
25 ans = None
26 while n1 or n2 or t:
27 v1 = n1.pop() if n1 else 0
28 v2 = n2.pop() if n2 else 0
29 t, v = divmod(v1 + v2 + t, 10)
30 node = ListNode(v)
31 node.next = ans
32 ans = node
33 return ans or ListNode(0)
]]>
2 #Runtime: 25 ms (Beats 49.62%)
3 #Memory: 13.6 MB (Beats 74.5%)
4
5 # Definition for a binary tree node.
6 # class TreeNode(object):
7 # def __init__(self, x):
8 # self.val = x
9 # self.left = None
10 # self.right = None
11
12 class Solution(object):
13 def distanceK(self, root, target, k):
14 """
15 :type root: TreeNode
16 :type target: TreeNode
17 :type k: int
18 :rtype: List[int]
19 """
20 graph = {}
21 dep_target = 0
22 q = deque([[root, -1]])
23 while q:
24 node, f = q.popleft()
25 if node.val not in graph:
26 if f != -1:
27 graph[node.val] = [f]
28 graph[f].append(node.val)
29 else:
30 graph[node.val] = []
31 else:
32 graph[node.val].append(f)
33 graph[f].append(node.val)
34 if node.left:
35 q.append([node.left, node.val])
36 if node.right:
37 q.append([node.right, node.val])
38 q = deque([target.val])
39 dis = 0
40 ans = []
41 vis = set([target.val])
42 while q and dis < k:
43 sz = len(q)
44 while sz:
45 sz -= 1
46 node = q.popleft()
47 for i in graph[node]:
48 if i not in vis:
49 q.append(i)
50 vis.add(i)
51 dis += 1
52 if dis == k:
53 ans = list(q)
54 return ans
]]>
2 #Runtime: 1051 ms (Beats 45.18%)
3 #Memory: 29.9 MB (Beats 87.95%)
4
5 class Solution(object):
6 def kSmallestPairs(self, nums1, nums2, k):
7 """
8 :type nums1: List[int]
9 :type nums2: List[int]
10 :type k: int
11 :rtype: List[List[int]]
12 """
13 fg = set()
14 ans = []
15 hp = []
16 for i in range(min(len(nums1), k)):
17 heapq.heappush(hp, (nums1[i] + nums2[0], nums1[i], nums2[0], 0))
18 while k and hp:
19 _, i, j, idx = heapq.heappop(hp)
20 ans.append([i, j])
21 if idx < len(nums2) - 1:
22 heapq.heappush(hp, (i + nums2[idx + 1], i, nums2[idx + 1], idx + 1))
23 k -= 1
24 return ans
]]>
2 #Runtime: 1356 ms (Beats 44.93%)
3 #Memory: 21.8 MB (Beats 68.12%)
4
5 class Solution(object):
6 def totalCost(self, costs, k, candidates):
7 """
8 :type costs: List[int]
9 :type k: int
10 :type candidates: int
11 :rtype: int
12 """
13 left = costs[:candidates]
14 right = costs[max(candidates, len(costs) - candidates):]
15 heapify(left)
16 heapify(right)
17 ans = 0
18 i = candidates
19 j = len(costs) - candidates - 1
20 for _ in range(k):
21 if (not right) or (left and left[0] <= right[0]):
22 ans += heappop(left)
23 if i <= j:
24 heappush(left, costs[i])
25 i += 1
26 else:
27 ans += heappop(right)
28 if i <= j:
29 heappush(right, costs[j])
30 j -= 1
31 return ans
]]>
2 #Runtime: 173 ms (Beats 71.74%)
3 #Memory: 21.8 MB (Beats 51.9%)
4
5 class Solution:
6 def numOfWays(self, nums: List[int]) -> int:
7 MOD = 10 ** 9 + 7
8
9 def cal(seq):
10 if not seq:
11 return 1
12 root = seq[0]
13 l_tree = [num for num in seq if num < root]
14 r_tree = [num for num in seq if num > root]
15 return math.comb(len(l_tree) + len(r_tree), len(l_tree)) * cal(l_tree) * cal(r_tree) % MOD
16 return (cal(nums) - 1) % MOD
]]>
2 #Runtime: 44 ms (Beats 43.16%)
3 #Memory: 17.3 MB (Beats 71.85%)
4
5 # Definition for a binary tree node.
6 # class TreeNode(object):
7 # def __init__(self, val=0, left=None, right=None):
8 # self.val = val
9 # self.left = left
10 # self.right = right
11 class Solution(object):
12 def getMinimumDifference(self, root):
13 """
14 :type root: TreeNode
15 :rtype: int
16 """
17 self.ans = 100000
18 self.pre_val = None
19
20 def DFS(node):
21 if not node:
22 return
23 DFS(node.left)
24 if self.pre_val is not None:
25 self.ans = min(self.ans, node.val - self.pre_val)
26 self.pre_val = node.val
27 DFS(node.right)
28
29 DFS(root)
30 return self.ans
]]>
2 #Runtime: 1378 ms (Beats 42.86%)
3 #Memory: 42.3 MB (Beats 33.33%)
4
5 class Solution(object):
6 def maxScore(self, nums1, nums2, k):
7 """
8 :type nums1: List[int]
9 :type nums2: List[int]
10 :type k: int
11 :rtype: int
12 """
13 hp = []
14 ans, t_sum = 0, 0
15 for a, b in sorted(list(zip(nums1, nums2)), key=itemgetter(1), reverse=True):
16 t_sum += a
17 heappush(hp, a)
18 if len(hp) == k:
19 ans = max(ans, t_sum * b)
20 t_sum -= heappop(hp)
21 return ans
]]>
2 #Runtime: 95 ms (Beats 80.5%)
3 #Memory: 17.9 MB (Beats 17.87%)
4
5 class KthLargest(object):
6
7 def __init__(self, k, nums):
8 """
9 :type k: int
10 :type nums: List[int]
11 """
12 self.nums = nums
13 self.K = k
14 heapify(self.nums)
15 while len(self.nums) > self.K:
16 heappop(self.nums)
17
18 def add(self, val):
19 """
20 :type val: int
21 :rtype: int
22 """
23 if len(self.nums) < self.K:
24 heappush(self.nums, val)
25 elif val > self.nums[0]:
26 heapreplace(self.nums, val)
27 return self.nums[0]
28
29
30
31 # Your KthLargest object will be instantiated and called as such:
32 # obj = KthLargest(k, nums)
33 # param_1 = obj.add(val)
]]>
2 #Runtime: 1108 ms (Beats 78.1%)
3 #Memory: 72.2 MB (Beats 80.63%)
4
5 # Definition for singly-linked list.
6 # class ListNode(object):
7 # def __init__(self, val=0, next=None):
8 # self.val = val
9 # self.next = next
10 class Solution(object):
11 def pairSum(self, head):
12 """
13 :type head: Optional[ListNode]
14 :rtype: int
15 """
16 node_slow, node_fast = head, head
17 ans = 0
18 while node_fast and node_fast.next:
19 node_fast = node_fast.next.next
20 node_slow = node_slow.next
21 node_cur, node_pre = node_slow, None
22 while node_cur:
23 node_cur.next, node_pre, node_cur = node_pre, node_cur, node_cur.next
24 while node_pre:
25 ans = max(ans, head.val + node_pre.val)
26 node_pre = node_pre.next
27 head = head.next
28 return ans
]]>
2 #Runtime: 23 ms (Beats 24.1%)
3 #Memory: 13.4 MB (Beats 79.10%)
4
5 # Definition for singly-linked list.
6 # class ListNode(object):
7 # def __init__(self, val=0, next=None):
8 # self.val = val
9 # self.next = next
10 class Solution(object):
11 def swapPairs(self, head):
12 """
13 :type head: ListNode
14 :rtype: ListNode
15 """
16 pre = self
17 pre.next = head
18 while pre.next and pre.next.next:
19 t = pre.next
20 t_next = t.next
21 pre.next, t_next.next, t.next = t_next, t, t_next.next
22 pre = t
23 return self.next
]]>
2 #Runtime: 1265 ms (Beats 56.45%)
3 #Memory: 91.1 MB (Beats 69.35%)
4
5 class Solution(object):
6 def swapNodes(self, head, k):
7 """
8 :type head: ListNode
9 :type k: int
10 :rtype: ListNode
11 """
12 t = 1
13 head2, head3 = head, head
14 while t < k:
15 head2 = head2.next
16 head3 = head3.next
17 t += 1
18 n = t
19 while head3:
20 head3 = head3.next
21 n += 1
22 t = 0
23 head3 = head
24 while t < n - k - 1:
25 head3 = head3.next
26 t += 1
27 head2.val, head3.val = head3.val, head2.val
28 return head
]]>
2 #Runtime: 29 ms (Beats 83.46%)
3 #Memory: 13.9 MB (Beats 47.65%)
4
5 class Solution:
6 def lastStoneWeight(self, stones: List[int]) -> int:
7 hp = []
8 for st in stones:
9 heapq.heappush(hp, -st)
10 while len(hp) >= 2:
11 s1 = heapq.heappop(hp)
12 s2 = heapq.heappop(hp)
13 if s2 > s1:
14 heapq.heappush(hp, s1 - s2)
15 return abs(hp[0]) if hp else 0
]]>
2 #Runtime: 28 ms (Beats 72.48%)
3 #Memory: 14.9 MB (Beats 82.57%)
4
5 # Definition for a binary tree node.
6 # class TreeNode(object):
7 # def __init__(self, val=0, left=None, right=None):
8 # self.val = val
9 # self.left = left
10 # self.right = right
11 class Solution(object):
12 def widthOfBinaryTree(self, root):
13 """
14 :type root: TreeNode
15 :rtype: int
16 """
17 q = deque([root])
18 q_idx = deque([1])
19 ans = 0
20 while q:
21 s = len(q)
22 for i in range(s):
23 node = q.popleft()
24 x = q_idx.popleft()
25 if i == 0:
26 tp_min = x
27 if i == s - 1:
28 tp_max = x
29 if node.left:
30 q.append(node.left)
31 q_idx.append(2 * x - 1)
32 if node.right:
33 q.append(node.right)
34 q_idx.append(2 * x)
35 ans = max(ans, tp_max - tp_min + 1)
36 return ans
]]>