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 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 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 builder(): val = next(chunk_iter) if val == '#': return None node = TreeNode(int(val)) node.left = builder() node.right = builder() return node
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 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 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 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 deleteNode(self, root: TreeNode, key: int) -> TreeNode: if not root: return root if key > root.val: root.right = self.deleteNode(root.right, key) elif key < root.val: root.left = self.deleteNode(root.left, key) else: if not root.left and not root.right: root = None elif root.right: root.val = self.successor(root) root.right = self.deleteNode(root.right, root.val) else: root.val = self.predecessor(root) root.left = self.deleteNode(root.left, root.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 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 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 buildPreOrder(): val = data_list.pop(0) if val == "#": return None node = TreeNode(int(val)) node.left = buildPreOrder() node.right = buildPreOrder() return node
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 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 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 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(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 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 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 addOneRow(self, root, v, d): """ """ if d in (0, 1): node = TreeNode(v) if d == 1: node.left = root else: node.right = root return node if root and d >= 2: root.left = self.addOneRow(root.left, v, d - 1 if d > 2 else 1) root.right = self.addOneRow(root.right, v, d - 1 if d > 2 else 0) return root
def allPossibleFBT(self, N: int) -> List[TreeNode]: if N % 2 == 0: return [] if N not in self.possible_map: results = [] for i in range(N): for left in self.allPossibleFBT(i): for right in self.allPossibleFBT(N - 1 - i): node = TreeNode(0) node.left = left node.right = right results.append(node) self.possible_map[N] = results return self.possible_map[N]
def pruneTree(self, root: TreeNode) -> TreeNode: """TODO 注意 if not root.left and not root.right and root.val == 0: return None 判断放在上面得出不了正确结果,思考下为什么 : 需要后序遍历 """ if not root: return None # if not root.left and not root.right and root.val == 0: # return None root.left = self.pruneTree(root.left) root.right = self.pruneTree(root.right) if not root.left and not root.right and root.val == 0: return None return root
def generateTreesRecursive(start, end): if start > end: return [None] all_trees = [] for i in range(start, end + 1): # all possible left subtrees if i is choosen to be a root left_trees = generateTreesRecursive(start, i - 1) # all possible right subtrees if i is choosen to be a root right_trees = generateTreesRecursive(i + 1, end) # connect left and right subtrees to the root i for l_tree in left_trees: for r_tree in right_trees: current = TreeNode(i) current.left = l_tree current.right = r_tree all_trees.append(current) return all_trees
def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode: node = TreeNode(val) if not root: return node pre, cur = None, root while cur: if cur.val > val: pre, cur = cur, cur.left elif cur.val < val: pre, cur = cur, cur.right else: break if val <= pre.val: node.left = pre.left pre.left = node else: node.right = pre.right pre.right = node return root
def splitBST(self, root: TreeNode, V: int) -> List[TreeNode]: """ GOOD 根节点要么在第一棵子树中,要么在第二棵子树中。假设根节点在第一棵子树中,那么这棵树的左子树一定在第一棵子树中, 右子树中部分节点在第一棵子树,部分在第二棵子树中 p0,p1为分割树的结果。 p0,p1 在分割之后依然还是二叉搜索树,其中p0 在第一棵子树中,p1 为第二棵子树 """ if not root: return [None, None] elif root.val <= V: # p0,p1 对应小于等于V的part,大于的 p0, p1 = self.splitBST(root.right, V) root.right = p0 return [root, p1] else: p0, p1 = self.splitBST(root.left, V) root.left = p1 return [p0, root]