def pathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: int """ if root is None: return 0 def travel(root, path_sum): ns = path_sum[-1] + root.val r = 0 for item in path_sum: if ns - item == sum: r += 1 path_sum.append(ns) if root.left is not None: r += travel(root.left, path_sum) if root.right is not None: r += travel(root.right, path_sum) path_sum.pop() return r return travel(root, [0]) if __name__ == "__main__": print(Solution().pathSum( build_binary_tree( ((((3, ), 3, (-2, )), 5, (2, (1, ))), 10, (-3, (11, )))), 8))
from data_structure import TreeNode, ds_print, build_binary_tree import sys class Solution: def maxPathSum(self, root: TreeNode) -> int: def dfs(root: TreeNode): if root is None: return -sys.maxsize, 0 left_ans, left_max_sum = dfs(root.left) right_ans, right_max_sum = dfs(root.right) return max(left_ans, right_ans, root.val + max(left_max_sum, 0) + max(right_max_sum, 0)), max( left_max_sum, right_max_sum, 0) + root.val return dfs(root)[0] if root is not None else 0 if __name__ == '__main__': print(Solution().maxPathSum(build_binary_tree("[1,2,3]"))) print(Solution().maxPathSum( build_binary_tree("[-10,9,20,null,null,15,7]"))) print(Solution().maxPathSum(build_binary_tree("[-3]"))) print(Solution().maxPathSum( build_binary_tree( "[9,6,-3,null,null,-6,2,null,null,2,null,-6,-6,-6]")))
from data_structure import TreeNode, build_binary_tree class Solution: def largestValues(self, root): """ :type root: TreeNode :rtype: List[int] """ result = [] def dfs(node, deep): if node is None: return if len(result) == deep: result.append(node.val) else: result[deep] = max(result[deep], node.val) dfs(node.left, deep + 1) dfs(node.right, deep + 1) dfs(root, 0) return result if __name__ == "__main__": print(Solution().largestValues( build_binary_tree((((5, ), 3, (3, )), 1, (2, (9, ))))))
from data_structure import TreeNode, build_binary_tree class Solution: def zigzagLevelOrder(self, root): """ :type root: TreeNode :rtype: List[List[int]] """ result = [] def dfs(node, deep): if node is None: return if deep == len(result): result.append([]) result[deep].append(node.val) dfs(node.left, deep + 1) dfs(node.right, deep + 1) dfs(root, 0) for index in range(1, len(result), 2): result[index].reverse() return result if __name__ == "__main__": print(Solution().zigzagLevelOrder( build_binary_tree(((9, ), 3, ((15, ), 20, (7, ))))))
from data_structure import TreeNode, build_binary_tree class Solution: def preorderTraversal(self, root): """ :type root: TreeNode :rtype: List[int] """ if root is None: return [] result = [root.val] result.extend(self.preorderTraversal(root.left)) result.extend(self.preorderTraversal(root.right)) return result if __name__ == "__main__": print(Solution().preorderTraversal(build_binary_tree((1, ((3,), 2)))))
from data_structure import TreeNode, build_binary_tree class Solution: def isBalanced(self, root): """ :type root: TreeNode :rtype: bool """ def get_deep(node): if node is None: return 0 left, right = get_deep(node.left), get_deep(node.right) if left is None or right is None: return None if abs(left - right) > 1: return None return max(left, right) + 1 return get_deep(root) is not None if __name__ == "__main__": print(Solution().isBalanced( build_binary_tree(((((4, ), 3), 2), 1, (2, (3, (4, ))))))) print(Solution().isBalanced( build_binary_tree(((9, ), 3, ((15, ), 20, (7, )))))) print(Solution().isBalanced( build_binary_tree(((((4, ), 3, (4, )), 2, (3, )), 1, (2, )))))
path = search(root, []) path.reverse() result = [] for length, node in enumerate(path): if K - length == 0: result.append(node.val) break if length == 0: result.extend(get_length(node.left, K - length - 1)) result.extend(get_length(node.right, K - length - 1)) elif node.left == path[length - 1]: result.extend(get_length(node.right, K - length - 1)) elif node.right == path[length - 1]: result.extend(get_length(node.left, K - length - 1)) # result.extend(get_length(node, K - length - 1)) return result if __name__ == "__main__": # root = build_binary_tree( # (((6,), 5, ((7,), 2, (4,))), 3, ((0,), 1, (8,))) # ) # target = root.left # K = 2 # print(Solution().distanceK(root, target, K)) root = build_binary_tree((((3, ), 1, (2, )), 0)) target = root.left.right K = 1 print(Solution().distanceK(root, target, K))
class Solution: def pruneTree(self, root): """ :type root: TreeNode :rtype: TreeNode """ def dfs(node): if node is None: return False left = dfs(node.left) right = dfs(node.right) if not left: node.left = None if not right: node.right = None return left or right or node.val == 1 if not dfs(root): root = None return root if __name__ == "__main__": root = build_binary_tree((1, ((0,), 0, (1,)))) ds_print(Solution().pruneTree(root)) root = build_binary_tree((((0,), 0, (0,)), 1, ((0,), 1, (1,)))) ds_print(Solution().pruneTree(root)) root = build_binary_tree(((((0,), 1), 1, (1,)), 1, ((0,), 0, (1,)))) ds_print(Solution().pruneTree(root))
if len(paths) == deep: paths.append([]) paths[deep].append(path) dfs(node.left, deep + 1, (path << 1) + 0, paths) dfs(node.right, deep + 1, (path << 1) + 1, paths) paths = [] dfs(root, 0, 0, paths) return max( map(lambda path_line: max(path_line) - min(path_line), paths)) + 1 # return paths if __name__ == "__main__": print(Solution().widthOfBinaryTree( build_binary_tree((((5, ), 3, (3, )), 1, (2, (9, )))))) print(Solution().widthOfBinaryTree( build_binary_tree((((5, ), 3, (3, )), 1)))) print(Solution().widthOfBinaryTree( build_binary_tree((( (5, ), 3, ), 1, (2, ))))) print(Solution().widthOfBinaryTree( build_binary_tree(((( (6, ), 5, ), 3), 1, (2, (9, (7, )))))))
from data_structure import TreeNode, build_binary_tree class Solution: def distributeCoins(self, root: TreeNode) -> int: def dfs(node: TreeNode) -> (int, int, int): if node is None: return 0, 0, 0 left_node_number, left_coin_number, left_result = dfs(node.left) right_node_number, right_coin_number, right_result = dfs(node.right) node_number, coin_number = left_node_number + right_node_number + 1, left_coin_number + right_coin_number + node.val return node_number, coin_number, left_result + right_result + abs(node_number - coin_number) return dfs(root)[2] if __name__ == "__main__": print(Solution().distributeCoins(build_binary_tree("[3,0,0]"))) print(Solution().distributeCoins(build_binary_tree("[0,3,0]"))) print(Solution().distributeCoins(build_binary_tree("[1,0,2]"))) print(Solution().distributeCoins(build_binary_tree("[1,0,0,null,3]")))
from data_structure import TreeNode, build_binary_tree class Solution: def sumNumbers(self, root): """ :type root: TreeNode :rtype: int """ def dfs(node, num): num = num * 10 + node.val if node.left is None and node.right is None: return num left, right = 0, 0 if node.left is not None: left = dfs(node.left, num) if node.right is not None: right = dfs(node.right, num) return left + right if root is None: return 0 else: return dfs(root, 0) if __name__ == "__main__": print(Solution().sumNumbers(build_binary_tree(((2, ), 1, (3, ))))) print(Solution().sumNumbers( build_binary_tree((((5, ), 9, (1, )), 4, (0, )))))
from data_structure import TreeNode, build_binary_tree, ds_print class Solution: def invertTree(self, root): """ :type root: TreeNode :rtype: TreeNode """ def invert(node): if node is None: return invert(node.left) invert(node.right) node.left, node.right = node.right, node.left invert(root) return root if __name__ == "__main__": ds_print(Solution().invertTree(build_binary_tree((((1,), 2, (3,)), 4, ((6,), 7, (9,))))))
from data_structure import TreeNode, build_binary_tree class Solution: def isSameTree(self, p: TreeNode, q: TreeNode) -> bool: if p is None or q is None: return q == p return self.isSameTree(p.left, q.left) and p.val == q.val and self.isSameTree(p.right, q.right) if __name__ == "__main__": print(Solution().isSameTree(build_binary_tree(((2,), 1, (3,))), build_binary_tree(((2,), 1, (3,))))) print(Solution().isSameTree(build_binary_tree(((2,), 1)), build_binary_tree((1, (2,))))) print(Solution().isSameTree(build_binary_tree(((2,), 1, (1,))), build_binary_tree(((1,), 1, (2,)))))
class Solution: def binaryTreePaths(self, root): """ :type root: TreeNode :rtype: List[str] """ if root is None: return [] result = [] def dfs(root, path): path.append(root.val) if root.left is None and root.right is None: result.append(path.copy()) else: if root.left is not None: dfs(root.left, path) if root.right is not None: dfs(root.right, path) path.pop() dfs(root, []) return list(map(lambda x: '->'.join(map(lambda y: str(y), x)), result)) if __name__ == "__main__": print(Solution().binaryTreePaths(build_binary_tree( ((2, (5, )), 1, (3, )))))
from data_structure import TreeNode, build_binary_tree from itertools import product class Solution: def rob(self, root): """ :type root: TreeNode :rtype: int """ def dp(node): if node is None: return [0, 0] left_dp, right_dp = dp(node.left), dp(node.right) return [ max(map(sum, product(left_dp, right_dp))), left_dp[0] + right_dp[0] + node.val ] return max(dp(root)) if __name__ == "__main__": print(Solution().rob(build_binary_tree(((2, (3, )), 3, (3, (1, )))))) print(Solution().rob(build_binary_tree( (((1, ), 4, (3, )), 3, (5, (1, ))))))
class Solution: def isValidBST(self, root): """ :type root: TreeNode :rtype: bool """ def middle_travel(root): if root is None: return [] result = [] result.extend(middle_travel(root.left)) result.append(root.val) result.extend(middle_travel(root.right)) return result t = middle_travel(root) for index in range(len(t) - 1): if t[index] >= t[index + 1]: return False return True if __name__ == "__main__": print(Solution().isValidBST(build_binary_tree(((1, ), 0)))) print(Solution().isValidBST(build_binary_tree(((1, ), 1)))) print(Solution().isValidBST(build_binary_tree(((1, ), 2, (3, ))))) print(Solution().isValidBST( build_binary_tree(((1, ), 5, ((3, ), 4, (6, )))))) print(Solution().isValidBST( build_binary_tree(((5, ), 10, ((6, ), 15, (20, ))))))
class Solution: def flipEquiv(self, root1: TreeNode, root2: TreeNode) -> bool: def equal(n1, n2): return n1 is None and n2 is None or n1 is not None and n2 is not None and n1.val == n2.val if root1 is None and root2 is None: return True elif root1 is None or root2 is None: return False result = True if equal(root1.left, root2.right) and equal(root1.right, root2.left): if root1.left is not None: result = result and self.flipEquiv(root1.left, root2.right) if root1.right is not None: result = result and self.flipEquiv(root1.right, root2.left) elif equal(root1.left, root2.left) and equal(root1.right, root2.right): if root1.left is not None: result = result and self.flipEquiv(root1.left, root2.left) if root1.right is not None: result = result and self.flipEquiv(root1.right, root2.right) else: result = False return result if __name__ == "__main__": print(Solution().flipEquiv(build_binary_tree("[]"), build_binary_tree("[]"))) print(Solution().flipEquiv( build_binary_tree("[1,2,3,4,5,6,null,null,null,7,8]"), build_binary_tree("[1,3,2,null,6,4,5,null,null,null,null,8]")))
from data_structure import TreeNode, build_binary_tree class Solution: def hasPathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: bool """ if root is None: return False if root.left is None and root.right is None: return sum == root.val else: return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum( root.right, sum - root.val) if __name__ == "__main__": tree = build_binary_tree( ((((None, 7, None), 11, (None, 2, None)), 4, None), 5, ((None, 13, None), 8, (None, 4, (None, 1, None))))) print(Solution().hasPathSum(tree, 22)) tree = build_binary_tree((None, -2, (None, -3, None))) print(Solution().hasPathSum(tree, 0))
from data_structure import TreeNode, build_binary_tree, ds_print class Solution: def flatten(self, root): """ :type root: TreeNode :rtype: void Do not return anything, modify root in-place instead. """ def flatten_append(node, append_node): if node is None: return append_node node.right = flatten_append( node.left, flatten_append(node.right, append_node)) node.left = None return node flatten_append(root, None) if __name__ == "__main__": root = build_binary_tree((((3, ), 2, (4, )), 1, (5, (6, )))) Solution().flatten(root) ds_print(root)
from data_structure import TreeNode, build_binary_tree, ds_print class Solution: def inorderTraversal(self, root): """ :type root: TreeNode :rtype: List[int] """ if root is None: return [] result = [] result.extend(self.inorderTraversal(root.left)) result.append(root.val) result.extend(self.inorderTraversal(root.right)) return result if __name__ == "__main__": print(Solution().inorderTraversal(build_binary_tree( (None, 1, ((None, 3, None), 2, None)) )))
class Solution: def recoverTree(self, root: TreeNode) -> None: """ Do not return anything, modify root in-place instead. """ nodes = MidOrderTravel().mid_order_traval(root) peak, foot = None, None for index, node in enumerate(nodes): if index != len(nodes) - 1 and (index == 0 or nodes[index - 1].val < node.val) and nodes[index + 1].val < node.val and peak is None: peak = node if index != 0 and nodes[index - 1].val > node.val and (index == len(nodes) - 1 or nodes[index + 1].val > node.val): foot = node peak.val, foot.val = foot.val, peak.val if __name__ == "__main__": # root = build_binary_tree('[1,3,null,null,2]') # Solution().recoverTree(root) # ds_print(root, mode='leetcode') # root = build_binary_tree('[3,1,4,null,null,2]') # Solution().recoverTree(root) # ds_print(root, mode='leetcode') root = build_binary_tree('[146,71,-13,55,null,231,399,321,null,null,null,null,null,-33]') Solution().recoverTree(root) ds_print(root, mode='leetcode')
from data_structure import TreeNode, build_binary_tree, ds_print, null class Solution: def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode: if root is None or root is not None and root.val < val: node = TreeNode(val) node.left = root return node root.right = self.insertIntoMaxTree(root.right, val) return root if __name__ == "__main__": ds_print(Solution().insertIntoMaxTree(root=build_binary_tree( [4, 1, 3, null, null, 2]), val=5)) ds_print(Solution().insertIntoMaxTree(root=build_binary_tree( [5, 2, 4, null, 1]), val=3)) ds_print(Solution().insertIntoMaxTree(root=build_binary_tree( [5, 2, 3, null, 1]), val=4))
from data_structure import TreeNode, build_binary_tree class Solution: def isSymmetric(self, root): """ :type root: TreeNode :rtype: bool """ def check_symmetric(r0, r1): if r0 is None and r1 is None: return True elif r0 is None or r1 is None or r0.val != r1.val: return False return check_symmetric(r0.left, r1.right) and check_symmetric( r1.left, r0.right) if root is None: return True return check_symmetric(root.left, root.right) if __name__ == "__main__": root = build_binary_tree((((3, ), 2, (4, )), 1, ((4, ), 2, (3, )))) print(Solution().isSymmetric(root)) root = build_binary_tree(((2, (3, )), 1, (2, (3, )))) print(Solution().isSymmetric(root))
class Solution: def pathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: List[List[int]] """ def search_path(r, v, p): result = [] if r is not None: if r.left is None and r.right is None: if v == r.val: item = p.copy() item.append(r.val) result.append(item) else: p.append(r.val) result.extend(search_path(r.left, v - r.val, p)) result.extend(search_path(r.right, v - r.val, p)) p.pop() return result return search_path(root, sum, []) if __name__ == "__main__": root = build_binary_tree( ((((7, ), 11, (2, )), 4), 5, ((13, ), 8, ((5, ), 4, (1, ))))) print(Solution().pathSum(root, 22))
from data_structure import TreeNode, build_binary_tree class Solution: def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': def dfs(node, p, q): if node is None: return 0, None lcode, lnode = dfs(node.left, p, q) if lcode == 2: return lcode, lnode rcode, rnode = dfs(node.right, p, q) if rcode == 2: return rcode, rnode code = (node.val in {p, q}) + lcode + rcode if code == 2: return code, node return code, None return dfs(root, p.val, q.val)[1] if __name__ == "__main__": print(Solution().lowestCommonAncestor( build_binary_tree('[3,5,1,6,2,0,8,null,null,7,4]'), 5, 1).val) print(Solution().lowestCommonAncestor( build_binary_tree('[3,5,1,6,2,0,8,null,null,7,4]'), 5, 4).val)
from data_structure import TreeNode, build_binary_tree class Solution: def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': if p.val > q.val: p, q = q, p while (not (p.val <= root.val <= q.val)): if q.val < root.val: root = root.left else: root = root.right return root if __name__ == "__main__": def find(node, val): if node is None: return None if node.val == val: return node return find(node.left, val) or find(node.right, val) tree = build_binary_tree("[6,2,8,0,4,7,9,null,null,3,5]") print(Solution().lowestCommonAncestor(tree, find(tree, 2), find(tree, 8)).val) print(Solution().lowestCommonAncestor(tree, find(tree, 2), find(tree, 4)).val)
from data_structure import TreeNode, build_binary_tree class Solution: def averageOfLevels(self, root): """ :type root: TreeNode :rtype: List[List[int]] """ result = [] def dfs(node, deep): if node is None: return if deep == len(result): result.append([]) result[deep].append(node.val) dfs(node.left, deep + 1) dfs(node.right, deep + 1) dfs(root, 0) return list(map(lambda x: sum(x) / len(x), result)) if __name__ == "__main__": print(Solution().averageOfLevels(build_binary_tree( ((9,), 3, ((15,), 20, (7,))) )))