def backtrack(start: int, end: int) -> TreeNode: """ Recursively generate TreeNode by sub list of preorder. :param start: the start index of sub list. :param end: the end index of sub list. :return: the generated TreeNode. """ val = preorder[start] root = TreeNode(val) if start == end: return root i = start + 1 split = -1 has_split = False # find the split num of the sub list if it exists. while i <= end: if preorder[i] > val: has_split = True split = i break i += 1 if has_split: if start + 1 <= split - 1: # process the left children TreeNode root.left = backtrack(start + 1, split - 1) if split <= end: # process the right children TreeNode root.right = backtrack(split, end) else: if start + 1 <= end: # process the left children TreeNode root.left = backtrack(start + 1, end) return root
def test(self): solution = Solution() treeNode1 = TreeNode(5) treeNode1.left = TreeNode(3) treeNode1.left.left = TreeNode(2) treeNode1.left.right = TreeNode(4) treeNode1.right = TreeNode(6) treeNode1.right.right = TreeNode(7) self.assertEqual(solution.findTarget(treeNode1, 9), True) treeNode2 = TreeNode(5) treeNode2.left = TreeNode(3) treeNode2.left.left = TreeNode(2) treeNode2.left.right = TreeNode(4) treeNode2.right = TreeNode(6) treeNode2.right.right = TreeNode(7) self.assertEqual(solution.findTarget(treeNode2, 28), False) treeNode3 = TreeNode(2) treeNode3.left = TreeNode(1) treeNode3.right = TreeNode(3) self.assertEqual(solution.findTarget(treeNode3, 4), True) treeNode4 = TreeNode(1) self.assertEqual(solution.findTarget(treeNode4, 2), False) treeNode5 = TreeNode(0) treeNode5.left = TreeNode(-1) treeNode5.left.left = TreeNode(-3) treeNode5.right = TreeNode(2) treeNode5.right.right = TreeNode(4) self.assertEqual(solution.findTarget(treeNode5, -4), True)
def test(self): solution = Solution() treeNode1 = TreeNode(3) treeNode1.left = TreeNode(9) treeNode1.right = TreeNode(20) treeNode1.right.left = TreeNode(15) treeNode1.right.right = TreeNode(7) self.assertEqual(solution.sumOfLeftLeaves(treeNode1), 24) treeNode2 = TreeNode(1) treeNode2.left = TreeNode(2) treeNode2.left.left = TreeNode(4) treeNode2.left.right = TreeNode(5) treeNode2.right = TreeNode(3) self.assertEqual(solution.sumOfLeftLeaves(treeNode2), 4)
def addOneRow(self, root, v, d): """ :type root: TreeNode :type v: int :type d: int :rtype: TreeNode """ if d == 1: treeNode = TreeNode(v) treeNode.left = root return treeNode elif d == 2: left = root.left right = root.right root.left = TreeNode(v) root.right = TreeNode(v) root.left.left = left root.right.right = right return root else: if root.left: self.addOneRow(root.left, v, d - 1) if root.right: self.addOneRow(root.right, v, d - 1) return root
def generate_trees_2(self, n: int) -> List[TreeNode]: """ dp :param n: :return: """ def modify_tree(node: TreeNode, offset: int): if offset == 0 or not node: return node.val += offset modify_tree(node.left, offset) modify_tree(node.right, offset) # dp[i] stores the trees of n=i if n == 0: return [] dp = [[] for _ in range(n + 2)] dp[0].append(None) dp[1].append(TreeNode(1)) for i in range(2, n + 1): for j in range(1, i + 1): for left_tree in dp[j - 1]: for right_tree in dp[i - j]: root = TreeNode(j) root.left = left_tree new_right_tree = copy.deepcopy(right_tree) modify_tree(new_right_tree, j) root.right = new_right_tree dp[i].append(root) return dp[n]
def constructMaximumBinaryTree(self, nums): """ :type nums: List[int] :rtype: TreeNode """ rootTreeNode = None for num in nums: if rootTreeNode == None: rootTreeNode = TreeNode(num) currentTreeNode = rootTreeNode else: parentTreeNode = None currentTreeNode = rootTreeNode while currentTreeNode != None: if num > currentTreeNode.val: if parentTreeNode == None: newTreeNode = TreeNode(num) newTreeNode.left = rootTreeNode rootTreeNode = newTreeNode else: parentTreeNode.right = TreeNode(num) parentTreeNode.right.left = currentTreeNode break else: parentTreeNode = currentTreeNode currentTreeNode = currentTreeNode.right if currentTreeNode == None: parentTreeNode.right = TreeNode(num) return rootTreeNode
def test(self): solution = Solution() inTreeNode1 = TreeNode(4) inTreeNode1.left = TreeNode(2) inTreeNode1.right = TreeNode(7) inTreeNode1.left.left = TreeNode(1) inTreeNode1.left.right = TreeNode(3) outTreeNode1 = TreeNode(4) outTreeNode1.left = TreeNode(2) outTreeNode1.right = TreeNode(7) outTreeNode1.left.left = TreeNode(1) outTreeNode1.left.right = TreeNode(3) outTreeNode1.right.left = TreeNode(5) self.assertEqual( assertTreeNodeEqual(solution.insertIntoBST(inTreeNode1, 5), outTreeNode1), True)
def invert_tree(self, root: TreeNode) -> TreeNode: if not root: return None ans = TreeNode(root.val) ans.left = self.invert_tree(root.right) ans.right = self.invert_tree(root.left) return ans
def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode: if root and val < root.val: root.right = self.insertIntoMaxTree(root.right, val) return root newRoot = TreeNode(val) newRoot.left = root return newRoot
def recur(start: int, end: int) -> TreeNode: if start > end: return None mid = start + (end - start) // 2 root = TreeNode(nums[mid]) root.left = recur(start, mid - 1) root.right = recur(mid + 1, end) return root
def test(self): solution = Solution() treeNode1 = TreeNode(4) treeNode1.left = TreeNode(2) treeNode1.right = TreeNode(6) treeNode1.left.left = TreeNode(1) treeNode1.left.right = TreeNode(3) self.assertEqual(solution.minDiffInBST(treeNode1), 1)
def test(self): solution = Solution() treeNode1 = TreeNode(3) treeNode1.left = TreeNode(9) treeNode1.right = TreeNode(20) treeNode1.right.left = TreeNode(15) treeNode1.right.right = TreeNode(7) self.assertEqual(solution.averageOfLevels(treeNode1), [3, 14.5, 11])
def recur(node1: TreeNode, node2: TreeNode) -> TreeNode: if not node1: return node2 if not node2: return node1 result = TreeNode(node1.val + node2.val) result.left = recur(node1.left, node2.left) result.right = recur(node1.right, node2.right) return result
def recur(start: int, end: int) -> TreeNode: # [start, end) if end == start: return None mid = (start + end) // 2 root = TreeNode(nums[mid]) root.left = recur(start, mid) root.right = recur(mid + 1, end) return root
def test(self): solution = Solution() root = TreeNode(4) root.left = TreeNode(2) root.left.left = TreeNode(3) root.left.right = TreeNode(1) root.right = TreeNode(6) root.right.left = TreeNode(5) newRoot = TreeNode(4) newRoot.left = TreeNode(1) newRoot.left.left = TreeNode(2) newRoot.left.left.left = TreeNode(3) newRoot.left.left.right = TreeNode(1) newRoot.right = TreeNode(1) newRoot.right.right = TreeNode(6) newRoot.right.right.left = TreeNode(5) self.assertTrue( assertTreeNodeEqual(solution.addOneRow(root, 1, 2), newRoot))
def construct_maximum_binary_tree(self, nums: List[int]) -> TreeNode: if not nums: return None max_num = max(nums) max_index = nums.index(max_num) root = TreeNode(max(nums)) root.left = self.construct_maximum_binary_tree(nums[:max_index]) root.right = self.construct_maximum_binary_tree(nums[max_index + 1:]) return root
def test(self): solution = Solution() tree1 = TreeNode(1) tree1.left = TreeNode(0) tree1.right = TreeNode(2) tree1output = TreeNode(1) tree1output.right = TreeNode(2) self.assertEqual( assertTreeNodeEqual(solution.trimBST(tree1, 1, 2), tree1output), True)
def test(self): solution = Solution() treeNode1 = TreeNode(1) treeNode1.left = TreeNode(2) self.assertEqual(solution.printTree(treeNode1), [ ["", "1", ""], ["2", "", ""] ]) treeNode2 = TreeNode(1) treeNode2.left = TreeNode(2) treeNode2.right = TreeNode(3) treeNode2.left.right = TreeNode(4) self.assertEqual(solution.printTree(treeNode2), [ ["", "", "", "1", "", "", ""], ["", "2", "", "", "", "3", ""], ["", "", "4", "", "", "", ""] ]) treeNode3 = TreeNode(1) treeNode3.left = TreeNode(2) treeNode3.left.left = TreeNode(3) treeNode3.left.left.left = TreeNode(4) treeNode3.right = TreeNode(5) self.assertEqual(solution.printTree(treeNode3), [ ["", "", "", "", "", "", "", "1", "", "", "", "", "", "", ""], ["", "", "", "2", "", "", "", "", "", "", "", "5", "", "", ""], ["", "3", "", "", "", "", "", "", "", "", "", "", "", "", ""], ["4", "", "", "", "", "", "", "", "", "", "", "", "", "", ""] ]) treeNode4 = TreeNode(3) treeNode4.left = TreeNode(1) treeNode4.left.left = TreeNode(0) treeNode4.left.right = TreeNode(2) treeNode4.left.right.right = TreeNode(3) treeNode4.right = TreeNode(5) treeNode4.right.left = TreeNode(4) treeNode4.right.right = TreeNode(6) self.assertEqual(solution.printTree(treeNode4), [ ["", "", "", "", "", "", "", "3", "", "", "", "", "", "", ""], ["", "", "", "1", "", "", "", "", "", "", "", "5", "", "", ""], ["", "0", "", "", "", "2", "", "", "", "4", "", "", "", "6", ""], ["", "", "", "", "", "", "3", "", "", "", "", "", "", "", ""] ])
def build_tree(self, inorder: List[int], postorder: List[int]) -> TreeNode: root = TreeNode(postorder[-1]) pos = inorder.index(root.val) l_inorder = inorder[:pos] r_inorder = inorder[pos + 1:] if l_inorder: root.left = self.build_tree(l_inorder, postorder[:len(l_inorder)]) if r_inorder: root.right = self.build_tree( r_inorder, postorder[len(postorder) - len(r_inorder) - 1:-1]) return root
def build_tree(self, preorder: List[int], inorder: List[int]) -> TreeNode: if not preorder: return None root = TreeNode(preorder[0]) index = inorder.index(preorder[0]) left_tree_inorder = inorder[:index] right_tree_inorder = inorder[index + 1:] left_tree_preorder = preorder[1:index + 1] right_tree_preorder = preorder[index + 1:] root.left = self.build_tree(left_tree_preorder, left_tree_inorder) root.right = self.build_tree(right_tree_preorder, right_tree_inorder) return root
def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode: if not root: raise 'root is not defined' if val >= root.val: newRoot = TreeNode(val) newRoot.left = root return newRoot if root.right: root.right = self.insertIntoMaxTree(root.right, val) return root root.right = TreeNode(val) return root
def test(self): solution = Solution() rootTreeNode1 = TreeNode(6) rootTreeNode1.left = TreeNode(3) rootTreeNode1.left.right = TreeNode(2) rootTreeNode1.left.right.right = TreeNode(1) rootTreeNode1.right = TreeNode(5) rootTreeNode1.right.left = TreeNode(0) self.assertTrue( assertTreeNodeEqual( solution.constructMaximumBinaryTree([3, 2, 1, 6, 0, 5]), rootTreeNode1))
def upside_down_binary_tree(self, root: TreeNode) -> TreeNode: new_root = None parent_right_child = None # left child -> root, root -> right child, right child -> left child while root: left_child = root.left root.left = parent_right_child parent_right_child = root.right root.right = new_root new_root = root root = left_child return new_root
def backtrack(left: int, right: int) -> List[TreeNode]: if left > right: return [None] if left == right: return [TreeNode(left)] trees = [] for i in range(left, right + 1): for left_tree in backtrack(left, i - 1): for right_tree in backtrack(i + 1, right): root = TreeNode(i) root.left = left_tree root.right = right_tree trees.append(root) return trees
def increasing_bst3(self, root: TreeNode) -> TreeNode: dummy = cur = TreeNode(-1) stack = list() while root or stack: if root: stack.append(root) root = root.left else: root = stack.pop() root.left = None cur.right = root cur = cur.right root = root.right return dummy.right
def bstFromPreorder(self, preorder: List[int]) -> TreeNode: if len(preorder) == 0: return None if len(preorder) == 1: return TreeNode(preorder[0]) ans = TreeNode(preorder[0]) idx = 1 for i in range(1, len(preorder)): idx = i if preorder[i] > preorder[0]: break if preorder[idx] < preorder[0]: idx += 1 ans.left = self.bstFromPreorder(preorder[1:idx]) ans.right = self.bstFromPreorder(preorder[idx:]) return ans
def insertIntoBST(self, root, val): """ :type root: TreeNode :type val: int :rtype: TreeNode """ if root == None: root = TreeNode(val) return root if root.val > val: root.left = self.insertIntoBST(root.left, val) return root if root.val < val: root.right = self.insertIntoBST(root.right, val) return root return root
def allPossibleFBT(self, N): """ :type N: int :rtype: List[TreeNode] """ if N not in Solution.memo: ans = [] for x in range(N): y = N - 1 - x for left in self.allPossibleFBT(x): for right in self.allPossibleFBT(y): node = TreeNode(0) node.left = left node.right = right ans.append(node) Solution.memo[N] = ans return Solution.memo[N]
def sorted_list_to_bst_2(self, head: ListNode) -> TreeNode: """fast and slow pointer""" if not head: return None if not head.next: return TreeNode(head.val) prev, p, q = None, head, head while q and q.next: prev = p p = p.next q = q.next.next root = TreeNode(p.val) right = p.next prev.next = None root.left = self.sorted_list_to_bst_2(head) root.right = self.sorted_list_to_bst_2(right) return root
def flatten(self, root: TreeNode) -> None: """ Do not return anything, modify root in-place instead. """ if not root: return if not (root.left or root.right): return self.flatten(root.left) self.flatten(root.right) cur = root.left if not cur: return while cur.right: cur = cur.right cur.right = root.right root.right = root.left root.left = None