recur(node.right) recur(root) return result def sortedArrayToBST(self, nums: List[TreeNode]) -> TreeNode: def recur(left: int, right: int) -> TreeNode: if left == right: return None mid: int = int((left + right) / 2) root: TreeNode = nums[mid] root.left = recur(left, mid) root.right = recur(mid + 1, right) return root return recur(0, len(nums)) def balanceBST(self, root: TreeNode) -> TreeNode: l = self.inorderTraversal(root) r = self.sortedArrayToBST(l) return r root = TreeUtil.creat_tree([1,"null",2,"null",3,"null",4,"null","null"]) a = Solution().balanceBST(root)
result = [] q = queue.Queue() next_q = queue.Queue() sum = 0 count = 0 q.put(root) while not q.empty(): node: TreeNode = q.get() sum += node.val count += 1 if node.left: next_q.put(node.left) if node.right: next_q.put(node.right) if q.empty(): result.append(sum/count) sum = 0 count = 0 q = next_q next_q = queue.Queue() return result root = TreeUtil.creat_tree([3,9,20,15,7]) a = Solution().averageOfLevels(root) print(a) # leetcode submit region end(Prohibit modification and deletion)
def dfs(node_t: TreeNode, node_l: ListNode): if node_t.val == node_l.val: if dfs2(node_t, node_l): return True l = dfs(node_t.left, node_l) if node_t.left else False r = dfs(node_t.right, node_l) if node_t.right else False return l or r def dfs2(node_t: TreeNode, node_l: ListNode) -> bool: if node_t.val == node_l.val and node_l.next is None: return True elif node_t.val != node_l.val: return False else: l = dfs2(node_t.left, node_l.next) if node_t.left else False r = dfs2(node_t.right, node_l.next) if node_t.right else False return l or r return dfs(root, head) r1 = TreeUtil().creat_tree([1, 4, 4, 'null', 2, 2, 'null', 1, 'null', 6, 8, 'null', 'null', 'null', 'null', 1, 3]) r2 = ListUtil().creat_tree([1,4,2,6,8]) a = Solution().isSubPath(r2, r1) print(a)
# self.left = None # self.right = None from foo import TreeNode from foo import TreeUtil class Solution: def __init__(self): self.last = -100000 self.flag = True def isValidBST(self, root: TreeNode) -> bool: def in_order(node: TreeNode): if not node: return in_order(node.left) # if not left, return True if node.val <= self.last: # 比上一个值小 说明发现了不是升序 self.flag = False self.last = node.val in_order(node.right) # compare now and father in_order(root) return self.flag root = TreeUtil.creat_tree([3, 2, 5, 1, 3, 'null', 'null']) a = Solution().isValidBST(root) print(a) # leetcode submit region end(Prohibit modification and deletion)
from foo import TreeNode from foo import TreeUtil class Solution: def __init__(self): self.m = float("-inf") # self.maxSum = float("-inf") def maxPathSum(self, root: TreeNode) -> int: def max_sum(node: TreeNode): if not node: return 0 left_max = max(max_sum(node.left), 0) right_max = max(max_sum(node.right), 0) node_sum = node.val + left_max + right_max self.m = max(self.m, node_sum) return max(left_max + node.val, right_max + node.val) # 只能上报左路径或者右路径中的一条 max_sum(root) return self.m root = TreeUtil.creat_tree([-10, 9, 20, 'null', 'null', 15, 7]) # root = TreeUtil.creat_tree([5, 4, 8, 11, 'null', 13, 4, 7, 2, 'null', 'null', 'null', 1]) a = Solution().maxPathSum(root) print(a) # leetcode submit region end(Prohibit modification and deletion)
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None from foo import TreeNode from foo import TreeUtil class Solution: def isSymmetric(self, root: TreeNode) -> bool: def is_mirror(node1: TreeNode, node2: TreeNode): if not node1 and not node2: return True if not node1 or not node2: return False if node1.val != node2.val: return False return is_mirror(node1.left, node2.right) and is_mirror( node1.right, node2.left) return is_mirror(root, root) # head = TreeUtil.creat_tree(['1', '2', '2', '3', '4', '4', '3']) head = TreeUtil.creat_tree([1, 2, 2, 'null', 3, 'null', 3]) a = Solution().isSymmetric(head) print(a) # leetcode submit region end(Prohibit modification and deletion)
res = [] to_find = [p, q] def in_order(now: list, node: TreeNode): if not node: return now.append(node) # 发现重合值 添加当前路径 if node in to_find: res.append(now.copy()) in_order(now, node.left) in_order(now, node.right) now.pop() return res in_order([], root) root1, root2 = res[0], res[1] i = 0 while i < min(len(root1), len(root2)) and root1[i] == root2[i]: i += 1 return root1[i - 1] root = TreeUtil.creat_tree([3, 5, 1, 6, 2, 0, 8, 'null', 'null', 7, 4]) l = root.left r = root.left.right.right a = Solution().lowestCommonAncestor(root, l, r) print(a.val) # leetcode submit region end(Prohibit modification and deletion)
class Solution: def rightSideView(self, root: TreeNode) -> List[int]: if not root: return [] out = deque() result: List[int] = [] out.append(root) while len(out) != 0: next_out = deque() for i in range(len(out)): node = out.pop() if node.left: next_out.appendleft(node.left) if node.right: next_out.appendleft(node.right) if len(out) == 0: result.append(node.val) out = next_out return result root = TreeUtil.creat_tree([1, 2, 3, 'null', 5, 'null', 4]) a = Solution().rightSideView(root) print(a) # leetcode submit region end(Prohibit modification and deletion)
# leetcode submit region begin(Prohibit modification and deletion) # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None from foo import TreeNode from foo import TreeUtil class Solution: def invertTree(self, root: TreeNode) -> TreeNode: def invert(node: TreeNode): if not node: return node invert(node.left) invert(node.right) temp = node.left node.left = node.right node.right = temp invert(root) return root roo = TreeUtil.creat_tree([4,2,7,1,3,6,9]) a = Solution().invertTree(roo) print(a) # leetcode submit region end(Prohibit modification and deletion)
next_print = [] for i in range(0, len(stack)): node = stack.pop() if direct == 'right': if node.right: next_stack.append(node.right) next_print.append(node.right.val) if node.left: next_stack.append(node.left) next_print.append(node.left.val) elif direct == 'left': if node.left: next_stack.append(node.left) next_print.append(node.left.val) if node.right: next_stack.append(node.right) next_print.append(node.right.val) if next_print: result.append(next_print) stack = next_stack direct = 'left' if (direct == 'right') else 'right' return result # leetcode submit region end(Prohibit modification and deletion) t = TreeUtil().creat_tree([1, 2, 3, 4, 'null', 'null', 5]) a = Solution().zigzagLevelOrder(t) print(a)
deep(root) return self.m def diameterOfBinaryTree2(self, root): self.ans = 1 def depth(node): # 访问到空节点了,返回0 if not node: return 0 # 左儿子为根的子树的深度 L = depth(node.left) # 右儿子为根的子树的深度 R = depth(node.right) # 计算d_node即L+R+1 并更新ans # 用了一个变量 保存最大半径,这样只需要遍历考虑深度即可 self.ans = max(self.ans, L + R + 1) # 返回该节点为根的子树的深度 return max(L, R) + 1 depth(root) return self.ans - 1 root = TreeUtil.creat_tree([]) a = Solution().diameterOfBinaryTree2(root) print(a) # leetcode submit region end(Prohibit modification and deletion)
return False r = t.val def in_order(node: TreeNode): if not node: return in_order(node.left) if node.val == r: self.flag = self.flag or is_sub(node, t) in_order(node.right) def is_sub(node_l: TreeNode, node_r: TreeNode) -> bool: if not node_l and not node_r: return True if not node_l or not node_r or node_l.val != node_r.val: return False return is_sub(node_l.left, node_r.left) and is_sub(node_l.right, node_r.right) in_order(s) return self.flag root1 = TreeUtil().creat_tree([3, 4, 5, 1, 2, 'null', 'null']) root2 = TreeUtil().creat_tree([4, 1, 2]) a = Solution().isSubtree(root1, root2) print(a) # leetcode submit region end(Prohibit modification and deletion)
from foo import TreeNode from foo import TreeUtil class Solution: def hasPathSum(self, root: TreeNode, sum: int) -> bool: def helper(node: TreeNode, total: int): total = total + node.val # when it reach the leaf if not node.left and not node.right: return total == sum l = helper(node.left, total) if node.left else False r = helper(node.right, total) if node.right else False return l or r if not root: return False return helper(root, 0) root = TreeUtil.creat_tree([5,4,8,11,'null',13,4,7,2,'null','null','null',1]) a = Solution().hasPathSum(root, 22) print(a) # leetcode submit region end(Prohibit modification and deletion)