# 1 2 # / \ / \ # 3 2 1 3 # / \ \ # 5 4 7 # Output: # Merged tree: # 3 # / \ # 4 5 # / \ \ # 5 4 7 from leetcode.tree import TreeNode, lstToTreeNode, treeNodeToLst def mergeTrees(t1: TreeNode, t2: TreeNode) -> TreeNode: if t1 is None or t2 is None: return t1 or t2 t1.val += t2.val t1.left = mergeTrees(t1.left, t2.left) t1.right = mergeTrees(t1.right, t2.right) return t1 root = mergeTrees(lstToTreeNode([1, 3, 2, 5]), lstToTreeNode([2, 1, 3, None, 4, None, 7])) print(root) print(treeNodeToLst(root))
# 1008. Construct Binary Search Tree from Preorder Traversal # Return the root node of a binary search tree that matches the given preorder traversal. # (Recall that a binary search tree is a binary tree where for every node, # any descendant of node.left has a value < node.val, # and any descendant of node.right has a value > node.val. # Also recall that a preorder traversal displays the value of the node first, # then traverses node.left, then traverses node.right.) # Example 1: # Input: [8, 5, 1, 7, 10, 12] # Output: [8, 5, 10, 1, 7, null, 12] import bisect from leetcode.tree import treeNodeToLst, TreeNode def bstFromPreorder(A: 'List[int]') -> 'TreeNode': if not A: return None p = bisect.bisect(A, A[0]) root = TreeNode(A[0]) root.left = bstFromPreorder(A[1:p]) root.right = bstFromPreorder(A[p:]) return root print(treeNodeToLst(bstFromPreorder([8, 5, 1, 7, 10, 12]))) print(treeNodeToLst(bstFromPreorder([])))
# Note: # You may assume that duplicates do not exist in the tree. # # For example, given # # preorder = [3,9,20,15,7] # inorder = [9,3,15,20,7] # Return the following binary tree: # # 3 # / \ # 9 20 # / \ # 15 7 from leetcode.tree import treeNodeToLst, TreeNode def buildTree(preorder: 'List[int]', inorder: 'List[int]') -> 'TreeNode': if not preorder: return None val = preorder[0] root = TreeNode(val) p = inorder.index(val) root.left = buildTree(preorder[1:1 + p], inorder[:p]) root.right = buildTree(preorder[1 + p:], inorder[p + 1:]) return root print(treeNodeToLst(buildTree([3, 9, 6, 20, 15, 7], [6, 9, 3, 15, 20, 7])))
# Note: # You may assume that duplicates do not exist in the tree. # # For example, given # # inorder = [9,3,15,20,7] # postorder = [9,15,7,20,3] # Return the following binary tree: # # 3 # / \ # 9 20 # / \ # 15 7 from leetcode.tree import treeNodeToLst, TreeNode def buildTree(inorder: 'List[int]', postorder: 'List[int]') -> 'TreeNode': if not inorder: return None val = postorder[-1] root = TreeNode(val) p = inorder.index(val) root.left = buildTree(inorder[:p], postorder[:p]) root.right = buildTree(inorder[p + 1:], postorder[p:-1]) return root print(treeNodeToLst(buildTree([6, 9, 3, 15, 20, 7], [6, 9, 15, 7, 20, 3])))
def sortedListToBST1(head: 'ListNode') -> 'TreeNode': def link2Lst(head): cur = head result = [] while cur: result.append(cur.val) cur = cur.next return result def sortedArrayToBST(nums: 'List[int]') -> 'TreeNode': if not nums: return None p = len(nums) // 2 root = TreeNode(nums[p]) root.left = sortedArrayToBST(nums[:p]) root.right = sortedArrayToBST(nums[p + 1:]) return root if not head: return None nums = link2Lst(head) return sortedArrayToBST(nums) p = lst2link([-10, -3, 0, 5, 9]) print(treeNodeToLst(sortedListToBST(p))) p = lst2link([-10, -3, 0, 5, 9]) print(treeNodeToLst(sortedListToBST1(p)))
# For this problem, a height-balanced binary tree is defined as a binary tree # in which the depth of the two subtrees of every node never differ by more than 1. # # Example: # # Given the sorted array: [-10,-3,0,5,9], # # One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST: # # 0 # / \ # -3 9 # / / # -10 5 from leetcode.tree import treeNodeToLst, TreeNode def sortedArrayToBST(nums: 'List[int]') -> 'TreeNode': if not nums: return None p = len(nums)//2 root = TreeNode(nums[p]) root.left = sortedArrayToBST(nums[:p]) root.right = sortedArrayToBST(nums[p+1:]) return root tree = sortedArrayToBST([-10,-3,0,5, 9]) print(treeNodeToLst(tree))
# Explanation: # The above output corresponds to the 5 unique BST's shown below: # # 1 3 3 2 1 # \ / / / \ \ # 3 2 1 1 3 2 # / / \ \ # 2 1 2 3 from leetcode.tree import TreeNode, treeNodeToLst def generateTrees(n: int) -> 'List[TreeNode]': def generate(first, last): result = [] for root in range(first, last + 1): for left in generate(first, root - 1): for right in generate(root + 1, last): node = TreeNode(root) node.left = left node.right = right result.append(node) return result or [None] return generate(1, n) tr_lst = generateTrees(2) print([treeNodeToLst(x) for x in tr_lst]) print(len(tr_lst))