def main(): sln = Solution() root = TreeNode(2) a =TreeNode(1) b = TreeNode(3) root.left = a root.right = b print(sln.isValidBST(root))
def main(): sln = Solution() a = TreeNode(1) b = TreeNode(2) c = TreeNode(3) a.right = b b.left = c print(sln.inorderTraversal(a))
def build_tree(preorder, inorder): if inorder: index = inorder.index(preorder.pop(0)) node = TreeNode(inorder[index]) node.left = build_tree(preorder, inorder[0:index]) node.right = build_tree(preorder, inorder[index + 1:]) return node
def sorted_array_to_list(nums): if not nums: return None mid = len(nums) // 2 node = TreeNode(nums[mid]) node.left = sorted_array_to_list(nums[:mid]) node.right = sorted_array_to_list(nums[mid + 1:]) return node
def _generate_trees(self, start_inclusive: int, end_inclusive: int) -> List[TreeNode]: if start_inclusive > end_inclusive: return [None] if start_inclusive == end_inclusive: return [TreeNode(start_inclusive)] result = [] for i in range(start_inclusive, end_inclusive + 1): left_trees = self._generate_trees(start_inclusive, i - 1) right_trees = self._generate_trees(i + 1, end_inclusive) for lt in left_trees: for rt in right_trees: result.append(self._create_tree_node(i, lt, rt)) return result
def _search(self, node: TreeNode, level: int, result: List[List[int]]): if level >= len(result): result.append([node.val]) else: result[level].append(node.val) if node.left: self._search(node.left, level + 1, result) if node.right: self._search(node.right, level + 1, result) solution = Solution() root = TreeNode(3) root.left = TreeNode(9) root.right = TreeNode(20) root.right.left = TreeNode(15) root.right.right = TreeNode(7) assert solution.zigzagLevelOrder(root) == [[3], [20, 9], [15, 7]] root = TreeNode(3) root.left = TreeNode(9) root.right = TreeNode(20) root.left.left = TreeNode(2) root.left.right = TreeNode(4) root.right.left = TreeNode(15) root.right.right = TreeNode(7) assert solution.zigzagLevelOrder(root) == [[3], [20, 9], [2, 4, 15, 7]]
class Solution: def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode: return self._search_p_or_q(root, p, q) def _search_p_or_q(self, root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode: if not root or root.val == p.val or root.val == q.val: return root left = self._search_p_or_q(root.left, p, q) right = self._search_p_or_q(root.right, p, q) return root if (left and right) else (left or right) solution = Solution() r = TreeNode(3) r.left = TreeNode(5) r.right = TreeNode(1) r.left.left = TreeNode(6) r.left.right = TreeNode(2) r.right.left = TreeNode(0) r.right.right = TreeNode(8) r.left.right.left = TreeNode(7) r.left.right.right = TreeNode(4) assert solution.lowestCommonAncestor(r, TreeNode(5), TreeNode(1)).val == 3 assert solution.lowestCommonAncestor(r, TreeNode(7), TreeNode(4)).val == 2
def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode: if not root: return root if p.val < root.val and q.val < root.val: return self.lowestCommonAncestor(root.left, p, q) if p.val > root.val and q.val > root.val: return self.lowestCommonAncestor(root.right, p, q) return root solution = Solution() r = TreeNode(6) r.left = TreeNode(2) r.right = TreeNode(8) r.left.left = TreeNode(0) r.left.right = TreeNode(4) r.right.left = TreeNode(7) r.right.right = TreeNode(9) r.left.right.left = TreeNode(3) r.left.right.right = TreeNode(5) assert solution.lowestCommonAncestor(r, TreeNode(8), TreeNode(4)).val == 6 assert solution.lowestCommonAncestor(r, TreeNode(8), TreeNode(2)).val == 6 assert solution.lowestCommonAncestor(r, TreeNode(0), TreeNode(5)).val == 2 assert solution.lowestCommonAncestor(r, TreeNode(3), TreeNode(5)).val == 4
def _create_tree_node(self, value: int, left: TreeNode, right: TreeNode) -> TreeNode: t = TreeNode(value) t.left = left t.right = right return t
class Solution: def isValidBST(self, root: TreeNode) -> bool: return self._is_valid(root) def _is_valid( self, node: TreeNode, low_limit=float('-inf'), high_limit=float('inf')) -> bool: if not node: return True if node.val <= low_limit or node.val >= high_limit: return False return self._is_valid(node.left, low_limit, node.val) and self._is_valid( node.right, node.val, high_limit) solution = Solution() t = TreeNode(2) t.left = TreeNode(1) t.right = TreeNode(3) assert solution.isValidBST(t) t = TreeNode(5) t.left = TreeNode(1) t.right = TreeNode(4) t.right.left = TreeNode(3) t.right.right = TreeNode(6) assert not solution.isValidBST(t)