class Solution:
# @param root, a tree link node
# @return nothing
def connect(self, root):
if not root:
return
root.next = None
level = [root]
while level:
for i in range(len(level)1):
level[i].next = level[i+1]
level = [kid for l in level for kid in (l.left, l.right) if kid]
W
Windson
@Windson
1
Reputation
7
Posts
73
Profile views
0
Followers
0
Following
Posts made by Windson

Depend on level order use only 8 lines

RE: Simple O(n) without map
@StefanPochmann said in Simple O(n) without map:
searching in inorder, is O(n^2)
Why searching in inder( a list) cost O(n^2) instead of O(n)?

RE: Share my python solution with detailed explanation
@dasheng2 said in Share my python solution with detailed explanation:
2H + 2D = H + D + L > H + D = nL > H = nL  D
should be
2H + 2D = H + D + nL > H + D = nL > H = nL  D

RE: Python inplace solution with dummy head node.
@sarayamato for instance:
# dummy and head point to the same object dummy = head = ListNode(0) head.next = ListNode(1) head = head.next # return True print(dummy.next == head) # dummy.next points to head dummy = ListNode(0) head = ListNode(1) head.next = ListNode(2) dummy.next = head head = head.next # return 1 print(dummy.next.val)

Use Heap as priorityQueue
class Solution: def smallestRange(self, A): import heapq # create a heap for first element of each list, for example [0, 4, 5] lst = [(l[0], i, 0) for i, l in enumerate(A)] heapq.heapify(lst) left = min(l[0] for l in A) right = max(l[0] for l in A) # first answer will be [0, 5] ans = [left, right] while lst: left, a, b = heapq.heappop(lst) # check smallest range if (right  left) < (ans[1]  ans[0]) or \ ((right  left) == (ans[1]  ans[0]) and left < ans[0]): ans = [left, right] if b + 1 == len(A[a]): return ans # make sure the heaq will keep one element from each list heapq.heappush(lst, (A[a][b+1], a, b+1)) if A[a][b+1] > right: right = A[a][b+1]

Easy to understand solution, beat 95%
def numDecodings(self, S): # set default value is 1 answer = 1 prev_answer = 1 # the prev value is 1, 2 or not one, two = False, False for i in S: if i == '*': new = answer*9 if one: new += 9*prev_answer if two: new += 6*prev_answer one, two = True, True else: # drop it if meet 0 new = answer if (i > '0') else 0 if one: new += prev_answer if (two and i <= '6'): new += prev_answer one = (i == '1') two = (i == '2') prev_answer = answer answer = new % (10**9 + 7) return answer

RE: Simple Python solution (easy to understand)
class Solution(object): def threeSum(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ if len(nums) < 3: return [] nums.sort() s = set() for k, v in enumerate(nums): l, r = k+1, len(nums)1 while l < r: sums = v + nums[l] + nums[r] if sums == 0: s.add((v, nums[l], nums[r])) if nums[l] == nums[r]: break l += 1 r = 1 elif sums > 0: r = 1 elif sums < 0: l += 1 return list(s)
I guess this is what @ditya26 means.