def buildTree(l, r): if l > r: return None mid = (l + r) // 2 root = TreeNode(mem[mid]) root.left = buildTree(l, mid - 1) root.right = buildTree(mid + 1, r) return root
def buildSub(l1, r1, l2, r2): # print(l1, r1, l2, r2) if l1 > r1: return None root = TreeNode(preorder[l1]) if l1 == r1: return root nr = preorder[l1 + 1] i = l2 while postorder[i] != nr: i += 1 root.left = buildSub(l1 + 1, l1 + 1 + (i - l2), l2, i) root.right = buildSub(l1 + 1 + (i - l2) + 1, r1, i + 1, r2 - 1) return root
out.append([]) while len(stack1) > 0: node = stack1.pop() out[-1].append(node.val) if node.left != None: stack2.append(node.left) if node.right != None: stack2.append(node.right) if len(stack2) == 0: break out.append([]) while len(stack2) > 0: node = stack2.pop() out[-1].append(node.val) if node.right != None: stack1.append(node.right) if node.left != None: stack1.append(node.left) return out sl = Solution() root = TreeNode(3) root.left = TreeNode(9) root.left.left = TreeNode(1) root.left.right = TreeNode(3) root.right = TreeNode(20) root.right.left = TreeNode(15) root.right.right = TreeNode(7) print(sl.zigzagLevelOrder(root))