def test_is_balanced2(self): s = Solution() node1 = TreeNode(1) node2 = TreeNode(2) node3 = TreeNode(3) node4 = TreeNode(4) node2.left = node1 node2.right = node3 node1.left = node4 node4.left = TreeNode(5) self.assertFalse(s.isBalanced(node2))
def test_is_valid_bst2(self): s = Solution() node1 = TreeNode(1) node2 = TreeNode(2) node3 = TreeNode(3) node4 = TreeNode(4) node5 = TreeNode(5) node6 = TreeNode(6) node5.left = node1 node5.right = node4 node4.left = node3 node4.right = node6 self.assertFalse(s.isValidBST(node5))
def test_is_valid_bst(self): s = Solution() node1 = TreeNode(1) node2 = TreeNode(2) node3 = TreeNode(3) node2.left = node1 node2.right = node3 self.assertTrue(s.isValidBST(node2))
def test_is_balanced(self): s = Solution() node1 = TreeNode(1) node2 = TreeNode(2) node3 = TreeNode(3) node2.left = node1 node2.right = node3 self.assertTrue(s.isBalanced(node2))
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]: if not nums: return None mid = len(nums) // 2 root = TreeNode(nums[mid]) root.left = self.sortedArrayToBST(nums[:mid]) root.right = self.sortedArrayToBST(nums[mid + 1:]) return root
def test_is_valid_bst3(self): s = Solution() node0 = TreeNode(0) node1 = TreeNode(1) node2 = TreeNode(2) node3 = TreeNode(3) node4 = TreeNode(4) node5 = TreeNode(5) node6 = TreeNode(6) node3.left = node1 node3.right = node5 node5.left = node4 node5.right = node6 node1.left = node0 node1.right = node2 node2.right = TreeNode(3) self.assertFalse(s.isValidBST(node3))
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: if not inorder: return None v = preorder.pop(0) i = inorder.index(v) root = TreeNode(v) root.left = self.buildTree(preorder, inorder[:i]) root.right = self.buildTree(preorder, inorder[i + 1:]) return root
def test_binary_tree_paths(self): node1 = TreeNode(1) node2 = TreeNode(2) node3 = TreeNode(3) node5 = TreeNode(5) node1.left = node2 node1.right = node3 node2.right = node5 s = Solution() res = s.binaryTreePaths(node1) self.assertEqual(["1->2->5", "1->3"], res)
def vals_to_tree(self, vals: List[Optional[int]]) -> Optional[TreeNode]: if not vals: return None val = vals.pop(0) if val is None: return None root = TreeNode(val) root.left = self.vals_to_tree(vals) root.right = self.vals_to_tree(vals) return root
def test_lowest_common_ancestor(self): node0 = TreeNode(0) node1 = TreeNode(1) node2 = TreeNode(2) node3 = TreeNode(3) node4 = TreeNode(4) node5 = TreeNode(5) node6 = TreeNode(6) node7 = TreeNode(7) node8 = TreeNode(8) node3.left = node5 node3.right = node1 node5.left = node6 node5.right = node2 node1.left = node0 node1.right = node8 node2.left = node7 node2.right = node4 s = Solution() print(s.lowestCommonAncestor(node3, node5, node1).val) print(s.lowestCommonAncestor(node3, node5, node4).val)
def process(self, start: int, end: int) -> List[Optional[TreeNode]]: if start > end: return [None] res: List[TreeNode] = [] for i in range(start, end + 1): left_list = self.process(start, i - 1) right_list = self.process(i + 1, end) for left in left_list: for right in right_list: root = TreeNode(i) root.left = left root.right = right res.append(root) return res
def flatten(self, root: TreeNode) -> None: """ Do not return anything, modify root in-place instead. """ if not root: return self.flatten(root.left) self.flatten(root.right) tmp = root.right root.right = root.left root.left = None while root.right: root = root.right root.right = tmp
def process(self, head) -> Optional[TreeNode]: if not head: return None if not head.next: return TreeNode(head.val) # 先得到 链表的 left_mid 节点 left_mid = self.get_left_mid(head) mid = left_mid.next root = TreeNode(mid.val) # 将链表分为两段 left half and right half lh = head rh = mid.next left_mid.next = None # 递归 root.left = self.process(lh) root.right = self.process(rh) return root