def sortedArrayToBST(arr): if not arr: return None mid = len(arr) // 2 root = TreeNode(arr[mid]) root.left = sortedArrayToBST(arr[:mid]) root.right = sortedArrayToBST(arr[mid + 1:]) return root
def sorted_array_to_bst(array): if len(array) == 0: return None elif len(array) == 1: return TreeNode(array[0]) else: mid = int(len(array) / 2) root = TreeNode(array[mid]) root.left = sorted_array_to_bst(array[:mid]) root.right = sorted_array_to_bst(array[mid + 1:]) return root
def construct(inorder, preorder): if len(inorder) == 0 or len(preorder) == 0: return None else: root = preorder[0] index = inorder.index(root) left_inorder = inorder[:index] right_inorder = inorder[index + 1:] left_preorder = preorder[1:1 + len(left_inorder)] right_preorder = preorder[1 + len(left_inorder):] root = TreeNode(root) root.left = construct(left_inorder, left_preorder) root.right = construct(right_inorder, right_preorder) return root
def min_tree(arr): # base case if len(arr) is 0: return None # make node from middle of array mid = len(arr) / 2 node = TreeNode(arr[mid]) # recursively make node from each middle of each half of each array node.left = min_tree(arr[:mid]) node.right = min_tree(arr[mid + 1:]) # +1 to not include current mid # returns root return node
childNode = parentNode.right stack.pop() stack.append((parentNode, parentState - 1)) if childNode: stack.append((childNode, bothPending)) else: if oneNodeFound and len(stack) - 1 == fcaIndex: fcaIndex -= 1 stack.pop() return None if __name__ == "__main__": one = TreeNode(1) two = TreeNode(2) three = TreeNode(3) four = TreeNode(4) five = TreeNode(5) six = TreeNode(6) seven = TreeNode(7) eight = TreeNode(8) nine = TreeNode(9) one.left = two one.right = three two.left = four two.right = five three.left = six three.right = seven four.left = eight four.right = nine print(firstCommonAncestor(one, eight, five))
# left & right heights left_height = root.height(root.left) right_height = root.height(root.right) # get positive value of difference in height height_difference = abs(left_height - right_height) # check height requirement satisfied if height_difference <= 1: # ... recursively if check_balanced(root.left) is True and check_balanced( root.right) is True: return True # if we reach here means tree is not # height-balanced tree return False if __name__ == "__main__": bal_root = min_tree([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) print(check_balanced(bal_root)) unbal_root = TreeNode(5) unbal_root.left = TreeNode(4) unbal_root.left.left = TreeNode(3) unbal_root.left.left.left = TreeNode(2) unbal_root.left.left.left.left = TreeNode(1) unbal_root.right = TreeNode(6) print(check_balanced(unbal_root))
return result if __name__ == "__main__": fifty = TreeNode(50) twenty = TreeNode(20) sixty = TreeNode(60) ten = TreeNode(10) twentyFive = TreeNode(25) seventy = TreeNode(70) five = TreeNode(5) fifteen = TreeNode(15) sixtyFive = TreeNode(65) eighty = TreeNode(80) fifty.left = twenty fifty.right = sixty twenty.left = ten twenty.right = twentyFive sixty.right = seventy ten.left = five ten.right = fifteen seventy.left = sixtyFive seventy.right = eighty five = TreeNode(5) four = TreeNode(4) one = TreeNode(1) seven = TreeNode(7) six = TreeNode(6) ten = TreeNode(10) sol = Solution() print(sol.allSequences(fifty))
sol[level].append(node.value) if node.left: queue.append([node.left, level + 1]) if node.right: queue.append([node.right, level + 1]) return sol if __name__ == "__main__": one = TreeNode(1) two = TreeNode(2) three = TreeNode(3) four = TreeNode(4) five = TreeNode(5) six = TreeNode(6) seven = TreeNode(7) eight = TreeNode(8) one.left = two one.right = three two.left = four two.right = five three.left = six three.right = seven lists = listOfDepths(one) print(lists) for l in lists: curr = LinkedListNode(None) for i in lists[l]: curr.next = LinkedListNode(i) curr = curr.next
return True else: if root.val > max_val or root.val < min_val: return False else: if (root.left != None and root.val < root.left.val) or ( root.right != None and root.val > root.right.val): return False else: return isValidBST(root.left, root.val, min_val) and isValidBST( root.right, max_val, root.val) head = TreeNode.get_sample() print(isValidBST(head, sys.maxsize, -sys.maxsize - 1)) n1 = TreeNode(1) n2 = TreeNode(2) n3 = TreeNode(3) n4 = TreeNode(4) n5 = TreeNode(5) n6 = TreeNode(6) n7 = TreeNode(7) n4.left = n2 n4.right = n5 n2.right = n3 n2.left = n1 n5.right = n6 n3.right = n7 print(isValidBST(n4, sys.maxsize, -sys.maxsize - 1))
if root != parent.right: break root = parent parent = parent.parent return parent if __name__ == "__main__": twenty = TreeNode(20) eight = TreeNode(20) four = TreeNode(4) twelve = TreeNode(12) ten = TreeNode(10) fourteen = TreeNode(14) twentytwo = TreeNode(22) # setting Parents eight.parent = twenty four.parent = eight twelve.parent = eight ten.parent = twelve fourteen.parent = twelve twentytwo.parent = twenty # left right twenty.right = twentytwo twenty.left = eight eight.left = four eight.right = twelve twelve.left = ten twelve.right = fourteen print(successor(fourteen).value)
return True elif not r1 or not r2: return False elif r1.value != r2.value: return False else: return sameTree(r1.left, r2.left) and sameTree(r1.right, r2.right) if __name__ == "__main__": one = TreeNode(1) two = TreeNode(2) three = TreeNode(3) four = TreeNode(4) five = TreeNode(5) six = TreeNode(6) seven = TreeNode(7) eight = TreeNode(8) one.left = two one.right = three two.left = four two.right = five three.left = six three.right = seven r1 = one newOne = TreeNode(2) newOne.left = TreeNode(4) newOne.right = TreeNode(5) r2 = newOne print(checkSubtree(r1, r2))