from collections import deque from base.tree.tree_node import TreeNode class Solution: def levelOrder(self, root: TreeNode): if root is None: return [] result, queue = [], [root] while queue: next_level, curr_vals = [], [] for node in queue: if node.val is not None: curr_vals.append(node.val) if node.left: next_level.append(node.left) if node.right: next_level.append(node.right) queue = next_level result.append(curr_vals) return result if __name__ == '__main__': tree = TreeNode.from_list([3, 9, 20, None, None, 15, 7]) print(tree) sol = Solution() t = sol.levelOrder(tree) print(t)
class Solution: def isUnivalTree(self, root: TreeNode) -> bool: res = [] def dfs(root): queue = [root] while queue: element = queue.pop(0) res.append(element.val) if element.left: dfs(root.left) if element.right: dfs(root.right) dfs(root) return len(set(res)) == 1 if __name__ == '__main__': sol = Solution() tree = TreeNode.from_list([ 2, 2, 2, 2, 5, ]) print(tree) print(sol.isUnivalTree(tree))
from base.tree.tree_node import TreeNode class Solution: def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode: if root is None: return None if root == p or root == q: return root left = self.lowestCommonAncestor(root.left, p, q) right = self.lowestCommonAncestor(root.right, p, q) if left and right: return root if left is None and right is None: return None if left is None: return right else: return left if __name__ == '__main__': root = [6, 2, 8, 0, 4, 7, 9, None, None, 3, 5] p = 2 q = 8 tree = TreeNode.from_list(root) print(tree)
# Definition for a binary tree node. from base.tree.tree_node import TreeNode class Solution: def deleteNode(self, root: TreeNode, key: int) -> TreeNode: pass if __name__ == '__main__': b = [5, 3, 6, 2, 4, None, 7] t = TreeNode.from_list(b) print(t)
from base.tree.tree_node import TreeNode class Solution: def convertBST(self, root: TreeNode) -> TreeNode: pass if __name__ == '__main__': a = TreeNode.from_list( [4, 1, 6, 0, 2, 5, 7, None, None, None, 3, None, None, None, 8]) sol = Solution() print(sol.convertBST(a))
return True res, queue = [], [root] a = 1 while queue: next_level, curr_vals = [], [] for i in queue: if i and i.val is not None: curr_vals.append(i.val) if i.left: next_level.append(i.left) if i.right: next_level.append(i.right) queue = next_level if a % 2 == 1: if is_odd(curr_vals) is False: return False else: if is_even(curr_vals) is False: return False a += 1 return True if __name__ == '__main__': tree = TreeNode.from_list([1, 10, 4, 3, None, 7, 9, 12, 8, 6, None, None, 2]) sol = Solution() t = sol.isEvenOddTree(tree) print(t)
if root is None: return 0 result, queue = [], [root] while queue: next_level, vals = [], [] for node in queue: if node.val is not None: vals.append(node.val) if node.left: next_level.append(node.left) if node.right: next_level.append(node.right) queue = next_level result.append(vals) odd = 0 even = 0 for i, v in enumerate(result): if i % 2 == 0: even += sum(v) else: odd += sum(v) return max(odd, even) if __name__ == '__main__': sol = Solution() # tree = TreeNode.from_list([3, 2, 3, None, 3, None, 1]) tree = TreeNode.from_list([4, 1, None, 2, None, 3]) print(tree) print(sol.rob(tree))
from base.tree.tree_node import TreeNode from typing import Optional class Solution: def mirrorTree(self, root: TreeNode) -> Optional[TreeNode]: if not root: return root.left, root.right = self.mirrorTree(root.right), self.mirrorTree( root.left) return root if __name__ == '__main__': tree = TreeNode.from_list([4, 2, 7, 1, 3, 6, 9]) sol = Solution() a = sol.mirrorTree(tree) print(a)
# todo # 错误的,想仿照排列数和组合数进行回溯,因为是树型结构,但是没成功 class Solution: def pathSum(self, root: TreeNode, total: int) -> List[List[int]]: res = [] def dfs(root: TreeNode, total: int, one_ans): if sum(one_ans) == total: res.append(one_ans) if root.left: one_ans.append(root.val) dfs(root.left, total, one_ans) one_ans.pop() if root.right: one_ans.append(root.val) dfs(root.right, total, one_ans) one_ans.pop() one_ans = [] dfs(root, total, one_ans) return res if __name__ == '__main__': tree = TreeNode.from_list( [5, 4, 8, 11, None, 13, 4, 7, 2, None, None, None, None, 5, 1]) print(tree) sol = Solution() print(sol.pathSum(tree, 22))
from base.tree.tree_node import TreeNode class Solution: def findBottomLeftValue(self, root: TreeNode) -> int: level_values, queue = [], [root] while queue: next_level, curr_vals = [], [] for i in queue: if i.val is not None: curr_vals.append(i.val) if i.left: next_level.append(i.left) if i.right: next_level.append(i.right) queue = next_level level_values = curr_vals return level_values[0] if __name__ == '__main__': tree = TreeNode.from_list([2, 1, 3]) sol = Solution() print(sol.findBottomLeftValue(tree))