def addOneRow(self, root: TreeNode, v: int, d: int) -> TreeNode: """Me""" if not root or not d: return root level = 1 if d > 1: level_nodes = [] queue = [root] while queue: length = len(queue) if level == d - 1: level_nodes = queue break for i in range(length): node = queue.pop(0) if node.left: queue.append(node.left) if node.right: queue.append(node.right) level += 1 for node in level_nodes: tmp_left = node.left node.left = TreeNode(v) node.left.left = tmp_left tmp_right = node.right node.right = TreeNode(v) node.right.right = tmp_right else: node = TreeNode(v) node.left = root return node # print(level_nodes) return root
def minCameraCover(self, root: TreeNode) -> int: """ 后序遍历 贪心算法 Return 0 if it's a leaf. Return 1 if it's a parent of a leaf, with a camera on this node. Return 2 if it's coverd, without a camera on this node. """ UNCOVERED, CAMERA, COVERED = range(3) self.ans = 0 dummy = TreeNode(-1) dummy.left = root def dfs(node): if not node: return COVERED left = dfs(node.left) right = dfs(node.right) if left == UNCOVERED or right == UNCOVERED: self.ans += 1 return CAMERA elif left == CAMERA or right == CAMERA: return COVERED else: return UNCOVERED dfs(dummy) return self.ans
def build(): nonlocal i if i == len(s): return val = '' # 提取数字 while i < len(s) and (s[i].isdigit() or s[i] == '-'): val += s[i] i += 1 # 建立根节点 root = TreeNode(val) # 如果有左子树 if i < len(s) and s[i] == '(': # 跳过左括号 i += 1 root.left = build() # 跳过右括号 i += 1 # 如果有右子树 if i < len(s) and s[i] == '(': # 跳过左括号 i += 1 root.right = build() # 跳过右括号 i += 1 return root
def test_solution2(): root = TreeNode(-1, left=TreeNode(-1, TreeNode(-1), TreeNode(-1)), right=TreeNode(-1)) findElements = FindElements(root) assert findElements.find(1) assert findElements.find(3) assert not findElements.find(5)
def rebuild(l): if l[0] == "None": l.pop(0) return None root = TreeNode(l.pop(0)) root.left = rebuild(l) root.right = rebuild(l) return root
def helper(l, r): if l == r: return None mid = (l + r) // 2 root = TreeNode(nums[mid]) root.left = helper(l, mid) root.right = helper(mid + 1, r) return root
def test_solution3(): root = TreeNode(-1, right=TreeNode(-1, left=TreeNode(-1, left=TreeNode(-1)))) findElements = FindElements(root) assert findElements.find(2) assert not findElements.find(3) assert not findElements.find(4) assert findElements.find(5)
def invertTree(self, root: TreeNode) -> TreeNode: if not root: return root left = self.invertTree(root.right) right = self.invertTree(root.left) root.left = left root.right = right return root
def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode: node = TreeNode(val) if not root: return node if val > root.val: root.right = self.insertIntoBST(root.right, val) else: root.left = self.insertIntoBST(root.left, val) return root
def helper(start, end): if start > end: return None mid = (start + end) >> 1 root = TreeNode(nums[mid]) root.left = helper(start, mid - 1) root.right = helper(mid + 1, end) return root
def buildTree(self, preorder, inorder): if not inorder: return None # inorder is empty root = TreeNode(preorder[0]) root_pos = inorder.index(preorder[0]) root.left = self.buildTree(preorder[1: 1 + root_pos], inorder[: root_pos]) root.right = self.buildTree(preorder[root_pos + 1:], inorder[root_pos + 1:]) return root
def sortedArrayToBstHelper(arr, i, j): if i >= j: return None mid = (i + j) // 2 node = TreeNode(arr[mid]) node.left = sortedArrayToBstHelper(arr, i, mid) node.right = sortedArrayToBstHelper(arr, mid + 1, j) return node
def builder(): val = next(chunk_iter) if val == '#': return None node = TreeNode(int(val)) node.left = builder() node.right = builder() return node
def buildPreOrder(): val = data_list.pop(0) if val == "#": return None node = TreeNode(int(val)) node.left = buildPreOrder() node.right = buildPreOrder() return node
def helper(l): if l[0] == "#": l.pop(0) return None root = TreeNode(l.pop(0)) root.left = helper(l) root.right = helper(l) return root
def removeLeafNodes(self, root: TreeNode, target: int) -> TreeNode: if not root: return None root.left = self.removeLeafNodes(root.left, target) root.right = self.removeLeafNodes(root.right, target) if root.left is None and root.right is None and root.val == target: return None return root
def mergeTrees(self, t1: TreeNode, t2: TreeNode) -> TreeNode: if not t1: return t2 if not t2: return t1 t = TreeNode(t1.val + t2.val) t.left = self.mergeTrees(t1.left, t2.left) t.right = self.mergeTrees(t1.right, t2.right) return t
def sufficientSubset(self, root: TreeNode, limit: int) -> TreeNode: if root.left == root.right: return None if root.val < limit else root if root.left: root.left = self.sufficientSubset(root.left, limit - root.val) if root.right: root.right = self.sufficientSubset(root.right, limit - root.val) return root if root.left or root.right else None
def flatten(self, root: TreeNode) -> None: if root is None: return None if root: self.flatten(root.right) self.flatten(root.left) root.right = self.prev root.left = None self.prev = root
def buildRecu(in_start, in_end): if in_start == in_end: return None val = preorder.pop(0) root = TreeNode(val) idx = lookup[val] root.left = buildRecu(in_start, idx) root.right = buildRecu(idx + 1, in_end) return root
def helper(start, end): # print(start, end) if start > end: return None max_val = max(nums[start:end + 1]) max_index = lookup[max_val] root = TreeNode(max_val) root.left = helper(start, max_index - 1) root.right = helper(max_index + 1, end) return root
def helper(in_start, in_end): if in_start == in_end: return None node_val = postorder.pop() node = TreeNode(node_val) index = inorder_lookup[node_val] # print("Node", node_val, in_start, in_end) node.right = helper(index + 1, in_end) node.left = helper(in_start, index) return node
def buildTreeRecurse(pre_start, in_start, in_end): if in_start == in_end: return None node_val = preorder[pre_start] node = TreeNode(node_val) idx = inorder_lookup[node_val] # print("pre_start,idx,in_start", pre_start,idx ,in_start) node.left = buildTreeRecurse(pre_start + 1, in_start, idx) # 得到左子树中的节点数目 idx - in_start node.right = buildTreeRecurse(pre_start + 1 + idx - in_start, idx + 1, in_end) return node
def helper(in_start, in_end): # nonlocal pre_idx if in_start == in_end: return None node_val = preorder.pop(0) node = TreeNode(node_val) index = inorder_lookup[node_val] # pre_idx += 1 node.left = helper(in_start, index) node.right = helper(index + 1, in_end) return node
def buildTreeRecursive(post_end, in_start, in_end): if in_start == in_end: return None node_val = postorder[post_end - 1] node = TreeNode(node_val) idx = inorder_lookup[node_val] # print("post_end,idx,in_start", post_end, idx, in_end) node.left = buildTreeRecursive(post_end - 1 - (in_end - idx - 1), in_start, idx) node.right = buildTreeRecursive(post_end - 1, idx + 1, in_end) return node
def helper(lower=-math.inf, upper=math.inf): if self.idx == N: return None val = preorder[self.idx] if val < lower or val > upper: return None self.idx += 1 root = TreeNode(val) root.left = helper(lower, val) root.right = helper(val, upper) return root
def test_solutions1(): sol = Solution() pA = TreeNode(4) qA = TreeNode(5, TreeNode(6), TreeNode(2, TreeNode(7), pA)) rootA = TreeNode(3, left=qA, right=TreeNode(1, TreeNode(0), TreeNode(8))) resA = sol.lowestCommonAncestor(rootA, pA, qA) assert repr(resA) == repr(qA)
def test_solution(SolutionCLS): root = TreeNode(7, TreeNode(3), TreeNode(15, TreeNode(9), TreeNode(20))) iterator = SolutionCLS(root) assert iterator.next() == 3 assert iterator.next() == 7 assert iterator.hasNext() == True assert iterator.next() == 9 assert iterator.hasNext() == True assert iterator.next() == 15 assert iterator.hasNext() == True assert iterator.next() == 20 assert iterator.hasNext() == False
def trimBST(self, root: TreeNode, L: int, R: int) -> TreeNode: if not root: return root elif root.val < L: return self.trimBST(root.right, L, R) elif root.val > R: return self.trimBST(root.left, L, R) else: root.left = self.trimBST(root.left, L, R) root.right = self.trimBST(root.right, L, R) return root
def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode: """TODO""" node = TreeNode(val) if not root: return node if val > root.val: node.left = root return node cur = root while cur.right and cur.right.val > val: cur = cur.right cur.right, node.left = node, cur.right return root