def createMinimalTree(arr, start, end): if start > end: return mid = (start + end) // 2 # the middle element should be the root for the tree to be balanced. root = TreeNode(arr[mid]) root.left = createMinimalTree(arr, start, mid - 1) root.right = createMinimalTree(arr, mid + 1, end) return root
def sortedArrayToBST(self, nums): """ :type nums: List[int] :rtype: TreeNode """ if not nums: return mid = len(nums) // 2 root = Node(nums[mid]) root.left = self.sortedArrayToBST(nums[:mid]) root.right = self.sortedArrayToBST(nums[mid + 1:]) return root
def mergeTrees(self, t1, t2): """ :type t1: TreeNode :type t2: TreeNode :rtype: TreeNode """ if not t1 and not t2: return None elif not t2: return t1 elif not t1: return t2 newNode = TreeNode(t1.data+t2.data) newNode.left = self.mergeTrees(t1.left, t2.left) newNode.right = self.mergeTrees(t1.right, t2.right) return newNode
3 / \ 9 20 / \ 15 7 return its depth = 3.''' from TreesBasicOperations import Node class Solution(object): def maxDepth(self, root): """ :type root: TreeNode :rtype: int """ if not root: return 0 depth = 1 + max(self.maxDepth(root.left), self.maxDepth(root.right)) return depth if __name__ == "__main__": new_node = Node(3) new_node.left = Node(9) new_node.right = Node(20) new_node.right.left = Node(15) new_node.right.right = Node(7) obj = Solution() print(obj.maxDepth(new_node))
class Solution(object): def rangeSumBST(self, root, L, R): """ :type root: TreeNode :type L: int :type R: int :rtype: int """ if not root: return 0 if root.data < L: return self.rangeSumBST(root.right, L, R) elif root.data > R: return self.rangeSumBST(root.left, L, R) else: return root.data + self.rangeSumBST( root.left, L, R) + self.rangeSumBST(root.right, L, R) if __name__ == "__main__": new_node = Node(10) new_node.left = Node(5) new_node.right = Node(15) new_node.left.left = Node(3) new_node.left.right = Node(7) new_node.right.right = Node(18) obj = Solution() L, R = 7, 15 print(obj.rangeSumBST(new_node, L, R))
def mergeTrees(self, t1, t2): """ :type t1: TreeNode :type t2: TreeNode :rtype: TreeNode """ if not t1 and not t2: return None elif not t2: return t1 elif not t1: return t2 newNode = TreeNode(t1.data+t2.data) newNode.left = self.mergeTrees(t1.left, t2.left) newNode.right = self.mergeTrees(t1.right, t2.right) return newNode if __name__ == "__main__": new_node1 = TreeNode(1) new_node1.left = TreeNode(2) new_node1.left.left = TreeNode(3) new_node2 = TreeNode(1) new_node2.right = TreeNode(2) new_node2.right.right = TreeNode(3) obj = Solution() node = obj.mergeTrees(new_node1, new_node2) node.printInOrder()
if heightDiff > 1: return math.inf else: return max(leftHeight, rightHeight) + 1 def checkBalanced(root): if checkHeight(root) != math.inf: return True else: return False if __name__ == "__main__": # check for balanced tree new_node = Node(40) new_node.insert(60) new_node.insert(10) new_node.insert(5) new_node.insert(15) new_node.insert(45) print(checkBalanced(new_node)) # check for unbalanced tree node1 = Node(1) node1.left = Node(2) node1.left.left = Node(3) node1.left.left.left = Node(4) node1.right = Node(2) node1.right.right = Node(3) node1.right.right.right = Node(4)
return list_ll def listOfDepths(root, list_ll, level): '''Create a list of linkedlist of each level''' if root is None: return # if linkedlist of this level doesn't exist in the list, create a new linkedlist and add it to the list. if len(list_ll) == level: ll = LinkedList() list_ll.append(ll) ll = list_ll[level] ll.insertAtEnd(root.data) listOfDepths(root.left, list_ll, level + 1) listOfDepths(root.right, list_ll, level + 1) if __name__ == "__main__": new_node = TreeNode(40) new_node.insert(60) new_node.insert(10) new_node.insert(5) new_node.insert(15) new_node.insert(45) list_linkedlist = getListOfDepths(new_node) for ll in list_linkedlist: ll.printElements() print("##")
def checkBST(root): return isBST(root, -math.inf, math.inf) def isBST(root, min, max): if root is None: return True # base condition if root.data < min or root.data > max: return False return (isBST(root.left, min, root.data-1) and isBST(root.right, root.data+1, max)) if __name__ == "__main__": new_node = Node(40) new_node.insert(60) new_node.insert(10) new_node.insert(5) new_node.insert(15) new_node.insert(45) new_node.insert(3) print(checkBST(new_node)) node1 = Node(100) node1.right = Node(60) node1.left = Node(100) print(checkBST(node1))