def addOneRow(self, root: TreeNode, v: int, d: int) -> TreeNode: """ Idea: First, Do BFS, when reaches a node with depth d - 1, save its two node, create two new node, and connect the original nodes to the new nodes """ if d == 1: new_root = TreeNode(v) new_root.left = root return new_root que = collections.deque([(root, 1)]) while que: curr, depth = que.pop() if depth == d - 1: left = curr.left right = curr.right curr.left = TreeNode(v) curr.right = TreeNode(v) curr.left.left = left curr.right.right = right else: if curr.left: que.appendleft((curr.left, depth + 1)) if curr.right: que.appendleft((curr.right, depth + 1)) return root
def dfs(node: TreeNode, parent: bool) -> Optional[TreeNode]: if node: delete = node.val in to_delete if not parent and not delete: res.append(node) node.left = dfs(node.left, not delete) node.right = dfs(node.right, not delete) return node if not delete else None
def _des(self, data: List[str]) -> Optional[TreeNode]: val = data.pop() if val == "None": return None else: root = TreeNode(int(val)) root.right = self._des(data) root.left = self._des(data) return root
def _dfs(self, node: TreeNode, limit: int, path_sum: int) -> int: current_sum = path_sum + node.val if not node.left and not node.right: return current_sum left = self._dfs(node.left, limit, current_sum) if node.left else -float("inf") right = self._dfs(node.right, limit, current_sum) if node.right else -float("inf") if left < limit: node.left = None if right < limit: node.right = None return max(left, right)
def _dec_helper(self, data: List[str], smaller, bigger) -> Optional[TreeNode]: if not data or int(data[-1]) > smaller or int(data[-1]) < bigger: return None else: root = TreeNode(int(data.pop())) root.right = self._dec_helper( data, smaller, root.val) # reverse process, right goes first root.left = self._dec_helper(data, root.val, bigger) return root
def recur(start, end) -> TreeNode: """ returns the root of subtree using inorder[start: end + 1] """ if start > end: return None sub_root = postorder.pop() mid = inorder_map[sub_root] curr = TreeNode(sub_root) curr.right = recur(mid + 1, end) curr.left = recur(start, mid - 1) return curr
def invertTree(self, root: TreeNode) -> TreeNode: """ idea: recursive call, post order traversal, this gives us the left-sub-tree and the right-sub-tree, switch them, then return the node """ if not root: return None left_sub_tree, right_sub_tree = None, None if root.left: left_sub_tree = self.invertTree(root.left) if root.right: right_sub_tree = self.invertTree(root.right) root.left, root.right = right_sub_tree, left_sub_tree return root
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': """ Idea: Find the first node that is in between p and q """ if p.val < q.val: # makes p.val >= q.val p, q = q, p while root: if q.val <= root.val <= p.val: return root elif root.val < q.val: root = root.right else: root = root.left return TreeNode()
class Solution: def trimBST(self, root: TreeNode, L: int, R: int) -> TreeNode: if not root: return root self.res = root def inorder(node, prev): if not node: return inorder(node.left, node) if node.val < L: node = node.right if node.val > R: node = node.left inorder(node.right, node) inorder(root, None) return self.res if __name__ == '__main__': arr = [3, 2, 4, 1] root = DS.arr2TreeNode(arr) node0 = TreeNode(0) node2 = TreeNode(2) node1 = TreeNode(1, node0, node2) sol = Solution() print(sol.trimBST(root, 1, 1)) print("S")