class Solution(object):
def sortedListToBST(self, head):
"""
:type head: ListNode
:rtype: TreeNode
"""
if not head:
return None
if not head.next:
return TreeNode(head.val)
slow, fast = head, head
prev = slow
while fast and fast.next:
prev = slow
slow = slow.next
fast = fast.next.next
node = slow # current mid node
next = node.next
prev.next = None # disconnect the prev half list
node.next = None # disconnect the next half list
treeNode = TreeNode(node.val)
treeNode.left = self.sortedListToBST(head)
treeNode.right = self.sortedListToBST(next)
return treeNode
angelvivienne
@angelvivienne
Posts made by angelvivienne

Easy Python : using prev node

RE: Suggestion on extra questions
@1337c0d3r oh thanks for the immediate reply and kind words!

RE: Suggestion on extra questions
Thanks for sharing the thoughts, they're really helpful!
Forgive me if I'm asking the stupid question here :) And this might be off this topic a bit. I have few experience on database design as I just switched my role from UI eng to fullstack, so my wondering goes to how to index timestamp in column? I referred at some related topics like creatinganindexonatimestamptooptimizequery it sounds to me the timestamp needs be
encoded
first to an integer as the index and thendecoded
to date format. As you mentioned we could delete the old (> 30days) URLs when run out of the storage, what if there is no qualified ones (e.g. all < 15 days), and by the assumption the index increments as the time does, should we change the strategy to delete the ones with bigger indexes?Appreciate for anyone can help here.

Python simple DFS solution
Python simple DFS solution, inspired by common topics like subset, combinations...
class Solution(object): def countArrangement(self, N): """ :type N: int :rtype: int """ if not N or N < 1: return 0 visited = [False]*N count = [0] self.helper(visited, N, 0, count) return count[0] def helper(self, visited, N, i, count): if i == N: count[0] += 1 return for n in range(N): divisible = ((n+1) % (i+1) == 0) or ((i+1) % (n+1)== 0) if not visited[n] and divisible: visited[n] = True self.helper(visited, N, i+1, count) visited[n] = False

Simple and short JAVA solution with iterator
 Put all iterator in a queue
 Keep track of the current iterator
 Check hasNext() and next() of current
public class Vector2D { Queue<Iterator<Integer>> queue; Iterator<Integer> current = null; public Vector2D(List<List<Integer>> vec2d) { queue = new LinkedList<Iterator<Integer>>(); for (int i = 0; i < vec2d.size(); i++){ queue.add(vec2d.get(i).iterator()); } current = queue.poll(); // first } public int next() { if (!current.hasNext()) return 1; return current.next(); } public boolean hasNext() { if (current == null) return false; while (!current.hasNext()) { if (!queue.isEmpty()) { current = queue.poll(); } else return false; } return true; } }

RE: AC clean Java solution
hi thanks for sharing,
what is the complexity of this algo?

Easy Java implementation

I divide the solution to this question into two parts:

one is for counting the valid number of words which can fit into one line, i.e. helper() function does it and also passes the next index to be traversed in the next turn (it can be modified as iterative way if you are more comfortable with).

the other part serves as a string editor, i.e. addList() uses the actual valid words lengths (len) and index of start (i, inclusive) and end (j, exclusive) to count the spaces to be added.
public class Solution { private List<String> result; public List<String> fullJustify(String[] words, int maxWidth) { result = new ArrayList<String>(); if (words == null  words.length == 0  maxWidth < 0) return result; if (maxWidth == 0) { result.add(""); return result; } helper(words, 0, maxWidth); return result; } public void helper(String[] words, int start, int L) { if (start >= words.length) return; int i = start, len = 0, total = 0, next = 1; while (total < L && i < words.length) { total += words[i].length(); if (total > L) { // only in this case we need skip i++ next = i; break; } len += words[i].length(); total++; // count space i++; } if (next == 1) next = i; addList(words, start, next, len, L); helper(words, next, L); } public void addList(String[] words, int i, int j, int len, int L) { StringBuilder sb = new StringBuilder(""); int count = ji1, space = 0, more = 0, s = 0; if (count == 0  j == words.length) { // the last line for (int k = i; k < j; k++) { sb.append(words[k]); if (k == j1) break; sb.append(" "); } space = L  sb.length(); s = 0; while (s++ < space) sb.append(" "); } else { space = (L  len) / count; more = (L  len) % count; for (int k = i; k < j; k++) { sb.append(words[k]); s = 0; if (k == j1) break; while (s++ < space) sb.append(" "); if (more > 0) sb.append(" "); } } result.add(sb.toString()); } }


RE: 3 ways implemented in JAVA (Python): Binary Search, inorder iterative & recursive
The best performance is we just have to count the nodes for once (i.e. kth is root), which is O(n); the worse/average case when we need count nodes for each subtree traversal, binary search is always log(n), and number of traversed subtrees could be n, then as total is O(nlog(n)).

RE: 3 ways implemented in JAVA (Python): Binary Search, inorder iterative & recursive
Yes you are right, the 3rd one is not BFS, it just uses stack. Both 2nd and 3rd are inorder traversal.