def deserialize_tree(s): if not s: return val = s.pop(0) root = None if val != 'null': root = TreeNode(val) root.left = deserialize_tree(s) root.right = deserialize_tree(s) return root
def build1(inorder, postorder): if not inorder: return None elif len(inorder) == 1: return TreeNode(inorder[0]) else: postorder_root_val = postorder[-1] root = TreeNode(postorder_root_val) for i in range(len(inorder)): if postorder_root_val == inorder[i]: inorder_root_index = i break if inorder_root_index == 0: root.left = None root.right = build1(inorder[1:], postorder[:-1]) elif inorder_root_index == len(inorder) - 1: root.left = build1(inorder[:-1], postorder[:-1]) root.right = None else: root.left = build1(inorder[:inorder_root_index], postorder[:inorder_root_index]) root.right = build1(inorder[inorder_root_index + 1:], postorder[inorder_root_index:-1]) return root
def build1(preorder, inorder): if not preorder: return None if len(preorder) <= 1: return TreeNode(preorder[0]) root_node = preorder[0] root = TreeNode(root_node) for i in range(len(inorder)): if root_node == inorder[i]: inorder_root_node_index = i break root.left = build1(preorder[1:inorder_root_node_index + 1], inorder[:inorder_root_node_index]) root.right = build1(preorder[inorder_root_node_index + 1:], inorder[inorder_root_node_index + 1:]) return root
def before_middle_construct_tree(before_seq, middle_seq): """ 根据二叉树的前序遍历和中序遍历重建二叉树 :param before_seq: List[int] :param middle_seq: List[int] :return: TreeNode """ if len(before_seq) == 0 or len(middle_seq) == 0: return root_node = TreeNode(before_seq[0]) root_index = middle_seq.index(root_node.val) left_node = before_middle_construct_tree(before_seq[1:root_index + 1], middle_seq[:root_index]) right_node = before_middle_construct_tree(before_seq[root_index + 1:], middle_seq[root_index + 1:]) root_node.left = left_node root_node.right = right_node return root_node
# 非递归方式解法,父亲节点的元组中第一个元素为0,孩子节点的元组中第一个元素为1 res = [] s = [(1, root)] # 0表示不可以往下遍历,1表示可以往下继续遍历 while s: p = s.pop() if not p[1]: # 判断某个节点为空 continue if p[0]: # 判断为子节点的话,就继续往栈里面放数据 s.extend([(1, p[1].right), (0, p[1]), (1, p[1].left)]) else: # 判断为主节点的话,就说明其左节点已经为空了,那么该父节点可以输出到数组中了 res.append(p[1].val) return res if __name__ == "__main__": """ 1 4 2 3 6 """ root = TreeNode(1) root.left = layer2_left = TreeNode(4) root.right = layer2_right = TreeNode(2) layer2_left.left = TreeNode(3) layer2_left.right = TreeNode(6) # print(tree_in_order_traversal(root)) # res = [] # tree_in_order_traversal1(root, res) # print(res) print(tree_in_order_traversal2(root))
elif nums[low] + nums[high] < k: low += 1 else: return True return False if __name__ == "__main__": """ 5 / \ 3 6 / \ \ 2 4 7 Target = 9 """ root = TreeNode(5) root.left = layer2_left = TreeNode(3) root.right = layer2_right = TreeNode(6) layer2_left.left = TreeNode(2) layer2_left.right = TreeNode(4) layer2_right.right = TreeNode(7) target = 9 print(two_sum_iv_input_bst(root, k=target))
# root = root.left # return res # return pre_traverse(root) # Solution3 def is_symmetrical(root1, root2): if not root1 and not root2: return True if not root1 or not root2: return False if root1.val != root2.val: return False return is_symmetrical(root1.left, root2.right) and is_symmetrical( root1.right, root2.left) if not root: return True return is_symmetrical(root.left, root.right) if __name__ == "__main__": root = TreeNode(1) root.left = layer2_left = TreeNode(4) root.right = layer2_right = TreeNode(4) layer2_left.right = layer3_left_left = TreeNode(6) layer2_left.left = layer3_left_right = TreeNode(3) layer2_right.right = layer3_right_left = TreeNode(3) layer2_right.left = layer3_right_right = TreeNode(6) print(symmetrical_tree(root))
return 0 left = max(helper(node.left), 0) # 如果有加和为负值的左路径,就直接省略了 right = max(helper(node.right), 0) # 如果有加和为负值的右路径,就直接省略了 self.res = max(self.res, left + right + node.val) return max(left, right) + node.val helper(root) return self.res if __name__ == "__main__": """ -10 20 9 15 7 上面的和最大路径就是 15-20-7,最大和为42 """ root = TreeNode(-10) root.left = layer2_left = TreeNode(20) root.right = layer2_right = TreeNode(9) layer2_left.left = layer3_left1 = TreeNode(15) layer2_left.right = layer3_right1 = TreeNode(7) layer2_right.left = None layer2_right.right = None layer3_left1.left = None layer3_left1.right = None layer3_right1.left = None layer3_right1.right = None solu = Solution() print(solu.binary_tree_maximum_path_sum(root))
def layerTraverse(root): node_a = [] if root: node_a.append(root) while node_a: root = node_a.pop(0) print(root.val) if root.left: node_a.append(root.left) if root.right: node_a.append(root.right) if __name__ == "__main__": root = TreeNode(1) root.left = layer2_left = TreeNode(2) root.right = layer2_right = TreeNode(3) layer2_left.left = layer31_left = TreeNode(4) layer2_left.right = layer31_right = TreeNode(5) layer2_right.left = layer32_left = TreeNode(6) layer2_right.right = layer32_right = TreeNode(7) layerTraverse(root) invertTree(root) print('\n') layerTraverse(root)
:return: List[int] """ def find_(root, target, path, cur_sum, res): cur_sum += root.val path.append(root.val) is_leaf = not root.left and not root.right if cur_sum == target and is_leaf: res.append(path + []) if root.left: find_(root.left, target, path, cur_sum, res) if root.right: find_(root.right, target, path, cur_sum, res) path.pop() # 跳出本次递归之前需要复原path cur_sum -= root.val # 跳出本次递归之前需要复原cur_sum if not root: return res = [] find_(root, target, [], 0, res) return res if __name__ == "__main__": root = TreeNode(10) root.left = layer2_left = TreeNode(5) root.right = layer2_right = TreeNode(12) layer2_left.left = TreeNode(4) layer2_left.right = TreeNode(7) print(find_path(root, 22))