:rtype: int """ self.queue = [[], []] self.count = [] self.queue[0].append(root) self.bfs(0) return self.count.index(max(self.count)) + 1 def bfs(self, i): self.count.append(0) currentQ = self.queue[i % 2] nextQ = self.queue[(i+1) % 2] nextQ.clear() while currentQ: item = currentQ.pop() self.count[i] += item.val if item.left: nextQ.append(item.left) if item.right: nextQ.append(item.right) if nextQ: self.bfs(i+1) if __name__ == "__main__": s = Solution() l = [1, 7, 0, 7, -8, None, None] l = [1, 7, 0, 7, -8, None, None, 4, None, None, None, 11] root = initial_tree(l) print(s.maxLevelSum(root))
elif op == 'left': parent.left = node.left else: parent.right = node.left else: p = node.right while p.left: p = p.left p.left = node.left if not op: root = node.right else: if op == 'left': parent.left = node.right else: parent.right = node.right node = None return root if __name__ == "__main__": s = Solution2() root = [5, 3, 6, 2, 4, None, 7] # root = [] # root = [3] root = [1, None, 2] root = initial_tree(root) key = 3 key = 2 print(s.deleteNode(root, key))
思路: 深度遍歷 廣度遍歷 """ from tree_util import initial_tree, TreeNode from typing import List class Solution: def rightSideView(self, root: TreeNode) -> List[int]: if not root: return [] queue = [(root, 0)] result = [] layout = -1 while queue: item = queue.pop(0) # 出隊 if item[1] != layout: layout = item[1] result.append(item[0].val) if item[0].right: queue.append((item[0].right, item[1] + 1)) if item[0].left: queue.append((item[0].left, item[1] + 1)) return result if __name__ == "__main__": s = Solution() l = [1, 2, 3, None, 5, None, 4] # [1, 3, 4] print(s.rightSideView(initial_tree(l)))
-3 9 / / -10 5 """ from typing import List from tree_util import TreeNode, initial_tree class Solution: def sortedArrayToBST(self, nums: List[int]) -> TreeNode: def helper(left, right): if left > right: return None # 总是选择中间位置左边的数字作为根节点 mid = (left + right) // 2 root = TreeNode(nums[mid]) root.left = helper(left, mid - 1) root.right = helper(mid + 1, right) return root return helper(0, len(nums) - 1) if __name__ == "__main__": s = Solution() nums = [-10, -3, 0, 5, 9] r = s.sortedArrayToBST(nums) assert r == initial_tree([0, -3, 9, -10, None, 5]), f"case 1 neq"
rightGain = maxGain(node.right) # 需要返回的该节点最大贡献度 (为单一子树) nodeGain = node.val + max(0, leftGain, rightGain) # 计算该节点的最大路径和要把两个子树都算上 nodeSum = node.val + max(0, leftGain) + max(0, rightGain) maxSum = max(maxSum, nodeSum) return nodeGain maxGain(root) return maxSum if __name__ == "__main__": s = Solution() # _list = [1, 2, 3] # root = initial_tree(_list) # assert s.maxPathSum(root) == 6, 'case 1' # _list = [-10, 9, 20, None, None, 15, 7] # root = initial_tree(_list) # assert s.maxPathSum(root) == 42, 'case 2' # _list = [1] # root = initial_tree(_list) # assert s.maxPathSum(root) == 1, 'case 3' _list = [5, 4, 8, 11, None, 13, 4, 7, 2, None, None, None, 1] root = initial_tree(_list) assert s.maxPathSum(root) == 48, f'case 4: {s.maxPathSum(root)} neq 48' _list = [9, 6, -3, None, None, -6, 2, None, None, 2, None, -6, -6, -6] root = initial_tree(_list) assert s.maxPathSum(root) == 16, f'case 5: {s.maxPathSum(root)} neq 16'
self.result = list() def findTilt(self, root): """ :type root: TreeNode :rtype: int """ self.walkTree(root) return sum(self.result) def walkTree(self, s): """ 迭代实现 :type s: TreeNode """ if s is None: return 0 left_sum = self.walkTree(s.left) right_sum = self.walkTree(s.right) s_node_value = abs(left_sum - right_sum) self.result.append(s_node_value) return s.val + left_sum + right_sum if __name__ == '__main__': # l = [-8, 3, 0, -8, None, None, None, None, -1, None, 8] l = [1, 2, 3] tree = initial_tree(l) s = Solution() print(s.findTilt(tree))