def minimalTree(sortedList: List) -> Optional[BinaryLeaf]: if len(sortedList) == 1: return BinaryLeaf(sortedList[0]) elif not sortedList: return None else: middleIndex = floor(len(sortedList) / 2) root = BinaryLeaf(sortedList[middleIndex]) root.leftChild = minimalTree(sortedList[:middleIndex]) root.rightChild = minimalTree(sortedList[middleIndex + 1:]) return root
def testImBalancedBinaryTree(self): root = BinaryLeaf(5) leftChild = BinaryLeaf(3) leftLeftChild = BinaryLeaf(1) leftRightChild = BinaryLeaf(7) root.leftChild = leftChild leftChild.leftChild = leftLeftChild leftChild.rightChild = leftRightChild self.assertFalse(isBalancedBinaryTree(root))
def testBalancedBinaryTree(self): root = BinaryLeaf(5) leftChild = BinaryLeaf(3) leftLeftChild = BinaryLeaf(1) leftRightChild = BinaryLeaf(7) rightChild = BinaryLeaf(8) rightLeftChild = BinaryLeaf(6) root.leftChild = leftChild root.rightChild = rightChild leftChild.leftChild = leftLeftChild leftChild.rightChild = leftRightChild rightChild.leftChild = rightLeftChild self.assertTrue(isBalancedBinaryTree(root))
def testIsInvalidBinarySearchTree(self): root = BinaryLeaf(5) leftChild = BinaryLeaf(3) leftLeftChild = BinaryLeaf(1) leftRightChild = BinaryLeaf(7) rightChild = BinaryLeaf(8) rightLeftChild = BinaryLeaf(6) root.leftChild = leftChild root.rightChild = rightChild leftChild.leftChild = leftLeftChild leftChild.rightChild = leftRightChild rightChild.leftChild = rightLeftChild self.assertTrue(isBinarySearchTree(root))
def testInOrderSuccessorForLeftChildLeftChildNode(self): root = BinaryLeaf(5) leftChild = BinaryLeaf(3) leftLeftChild = BinaryLeaf(1) leftRightChild = BinaryLeaf(4) rightChild = BinaryLeaf(8) rightLeftChild = BinaryLeaf(6) root.leftChild = leftChild leftChild.parent = root root.rightChild = rightChild rightChild.parent = root leftChild.leftChild = leftLeftChild leftLeftChild.parent = leftChild leftChild.rightChild = leftRightChild leftRightChild.parent = leftChild rightChild.leftChild = rightLeftChild rightLeftChild.parent = rightChild self.assertEqual(inOrderSuccessor(leftRightChild).info, 5)
def testIsSubTreeValidCase(self): root = BinaryLeaf(5) leftChild = BinaryLeaf(3) leftLeftChild = BinaryLeaf(1) leftRightChild = BinaryLeaf(7) rightChild = BinaryLeaf(8) rightLeftChild = BinaryLeaf(6) root.leftChild = leftChild root.rightChild = rightChild leftChild.leftChild = leftLeftChild leftChild.rightChild = leftRightChild rightChild.leftChild = rightLeftChild self.assertTrue(isSubTree(root, rightChild))
def listOfDepths(root: BinaryLeaf): depth = 0 queue = SimpleQueue() queue.put((depth, root)) # Use linkedList instead of lists linkedLists = {} while not queue.empty(): depth, leaf = queue.get() if depth not in linkedLists: linkedLists[depth] = [leaf.info] else: linkedLists[depth].append(leaf.info) if leaf.leftChild: queue.put((depth + 1, leaf.leftChild)) if leaf.rightChild: queue.put((depth + 1, leaf.rightChild)) return linkedLists if __name__ == '__main__': rootLeaf = BinaryLeaf(10) leftChild = BinaryLeaf(5) rightChild = BinaryLeaf(15) rootLeaf.leftChild = leftChild rootLeaf.rightChild = rightChild print(listOfDepths(rootLeaf))
result += getSumOfPathsForNode(leaf.leftChild, targetSum, currentSum) result += getSumOfPathsForNode(leaf.rightChild, targetSum, currentSum) return result def getTotalSumOfPaths(leaf: BinaryLeaf, targetSum: int) -> int: if not leaf: return 0 else: return getSumOfPathsForNode(leaf, targetSum) \ + getTotalSumOfPaths(leaf.leftChild, targetSum) \ + getTotalSumOfPaths(leaf.rightChild, targetSum) if __name__ == '__main__': root = BinaryLeaf(5) leftChild = BinaryLeaf(3) leftLeftChild = BinaryLeaf(-1) leftRightChild = BinaryLeaf(7) rightChild = BinaryLeaf(0) rightLeftChild = BinaryLeaf(2) root.leftChild = leftChild root.rightChild = rightChild leftChild.leftChild = leftLeftChild leftChild.rightChild = leftRightChild rightChild.leftChild = rightLeftChild