def postOrderEx(): tree = Tree() n1 = Node('A') # I n2 = Node('M') # N n3 = Node('A') # S n4 = Node('Z') # C n5 = Node('O') # R n6 = Node('N') # E n7 = Node(' ') n8 = Node(' ') n9 = Node('<') n0 = Node('3') n0.left = n6 n0.right = n9 n6.left = n1 n6.right = n5 n5.left = n2 n5.right = n4 n4.right = n3 n9.left = n8 n8.right = n7 tree.root = n0 return tree
def inOrderEx(): tree = Tree() n1 = Node("T") n2 = Node("H") n3 = Node("N") n4 = Node("A") n5 = Node("U") n6 = Node("Y") n7 = Node("K") n8 = Node("O") n6.left = n7 n6.right = n8 n5.left = n6 n3.left = n4 n3.right = n5 n2.left = n1 n2.right = n3 tree.root = n2 return tree
def createMinimalBST(array, start, end): if (end < start): return None mid = (start + end) // 2 node = Node(array[mid]) node.left = createMinimalBST(array, start, mid - 1) node.right = createMinimalBST(array, mid + 1, end) return node
def sortedArrToBST(arr,start,end): """ The inorder traverse of BFS is the same as the sorted array """ if start > end: return None if start == end: return Node(arr[start]) mid = (start + end)/2 + 1 root = Node(arr[mid]) root.left = sortedArrToBST(arr,start, mid-1) root.right = sortedArrToBST(arr, mid+1 , end) return root
def main(): # Driver code to build binary tree root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) print('###################') postOrder(root) print() print('###################') inOrder(root) print() print('###################') print(inOrder_stack(root)) print('###################') preOrder(root) print('###################') print(height(root)) print('###################') givenLevel(root, 3) print() givenLevel(root, 2) print() givenLevel(root, 1) print() givenLevel(root, 0) print('###################') levelOrder(root) print('###################') levelOrder_queue(root) print('###################') print(maxDepth(root)) print('###################') print(diameter(root))
from binaryTree import Node from treeTraverse import * node = Node(3) node.left = Node(20) node.right = Node(9) node.left.left = Node(7) node.left.right = Node(15) node_list = preorderTraverse(node) node_list.sort() n = len(node_list) print(node_list) def arrToBST(arr, root): """ just apply sorted array to BST """ if not root: return arrToBST(arr, root.left) root.val = arr[0] arr.pop(0) arrToBST(arr, root.right) def binTreeToBST(root): if not root: return None arr = inorderTraverse(root) arr.sort()
from binaryTree import Node t1 = Node(1) t1.left = Node(2) t1.right = Node(3) t2 = Node(1) t2.left = Node(2) t2.right = Node(3) def treeToList(t): if t == None: return [] result = [t.val] return result + treeToList(t.left) + treeToList(t.right) def isSameTree(t, s): t_list = treeToList(t) s_list = treeToList(s) return s_list == t_list def isSame(t, s): if not t and not s: return True if t and s: if t.val == s.val: return isSame(t.left, s.left) and isSame(t.right, s.right) return False
from binaryTree import Node from treeTraverse import * from levelTraverseTree import * def pathSum(root, target): if not root: return [] if not root.left and not root.right and root.val == target: return [[root.val]] temp = pathSum(root.left, target-root.val) + pathSum(root.right, target-root.val) return [[root.val] + i for i in temp] target = 22 root = Node(5) root.left = Node(4) root.right = Node(8) root.left.left = Node(11) root.left.left.left = Node(7) root.left.left.right = Node(2) root.right.left = Node(13) root.right.right = Node(4) root.right.right.right = Node(1) root.right.right.left = Node(5) print (pathSum(root, target))
from binaryTree import Node def invertTree(root): if root == None: return None tl = invertTree(root.left) tr = invertTree(root.right) root.right = tl root.left = tr return root node = Node(3) node.left = Node(9) node.right = Node(20) node.right.left = Node(15) node.right.left.right = Node(7) node.right.left.left = Node(7) node = invertTree(node) print(node.left.val)
return list = None if len(lists) == level: list = [] list.append(root) lists.append([]) else: list = lists[level] list.append(root) createLevelLinkedList(root.left, level + 1) createLevelLinkedList(root.right, level + 1) ten = TreeNode(10) eight = TreeNode(8) eight.left = TreeNode(6) eight.right = TreeNode(9) twelve = TreeNode(12) eight.left ten.left = eight ten.right = twelve sol = createLevelLinkedList(ten, 0) for i in lists: print(i)
from binaryTree import Node root = Node(1) root.left = Node(2) root.right = Node(3) root.right.left = Node(4) root.right.right = Node(5) # these are dfs of the tree def inorderTraverse(tree): """ left root right """ if not tree: return [] return inorderTraverse(tree.left) + [tree.val] + inorderTraverse( tree.right) def preorderTraverse(tree): """ root left right """ if not tree: return [] return [tree.val] + preorderTraverse(tree.left) + preorderTraverse( tree.right) def postorderTraverse(tree): """
from binaryTree import Node from treeTraverse import * node = Node(5) node.left=Node(3) node.right= Node(6) node.left.left = Node(2) node.left.right =Node(4) node.right.right = Node(7) target = 10 # use two sum in array node_list = inorderTraverse(node) def twoSum(arr,target): """ O(n) solution for two sum """ maxVal = max(arr) help_list= [0] * (maxVal +1) for j in range(len(arr)): need = target - arr[j] if need < maxVal: if help_list[need] == 1: return True help_list[arr[j]] += 1 return False print (twoSum(node_list, target)) # Do not use two sum in array
from binaryTree import Tree, Node if __name__ == "__main__": tree = Tree() n1 = Node("a") n2 = Node("+") n3 = Node("*") n4 = Node("b") n5 = Node("-") n6 = Node("/") n7 = Node("c") n8 = Node("d") n9 = Node("e") n6.left = n7 n6.right = n8 n5.left = n6 n5.right = n9 n3.left = n4 n3.right = n5 n2.left = n1 n2.right = n3 tree.root = n2