def build_from_traversals(preorder, inorder): if not preorder or not inorder: return None root_val = preorder[0] i = inorder.find(root_val) root = TreeNode(root_val) root.left = build_from_traversals(preorder[1:i+1], inorder[0:i]) root.right = build_from_traversals(preorder[i+1:], inorder[i+1:])
def build_from_traversals(preorder, inorder): if not preorder or not inorder: return None root_val = preorder[0] i = inorder.find(root_val) root = TreeNode(root_val) root.left = build_from_traversals(preorder[1:i + 1], inorder[0:i]) root.right = build_from_traversals(preorder[i + 1:], inorder[i + 1:])
def pruneTree(self, root: TreeNode) -> TreeNode: if not root: return None root.left = self.pruneTree(root.left) root.right = self.pruneTree(root.right) if root.val == 0 and not root.left and not root.right: return None return root
def makeBalancedTree(nums): if not nums: return None midpoint = len(nums) / 2 node = TreeNode(nums[midpoint]) node.left = solution(nums[:midpoint]) node.right = solution(nums[(midpoint + 1):]) return node
def create_by_pre_order(values): value = values.pop(0) if value == 0: return None node = TreeNode(value=value) node.left = create_by_pre_order(values) node.right = create_by_pre_order(values) return node
def build_coinflip_tree(k, value=""): ''' INPUT: int OUTPUT: TreeNode Return the root of a binary tree representing all the possible outcomes form flipping a coin k times. ''' node = TreeNode(value) if k != 0: node.left = build_coinflip_tree(k - 1, value + "H") node.right = build_coinflip_tree(k - 1, value + "T") return node
def build_coinflip_tree(k, value=""): """Return the root of a binary tree for flipping coin k times. Root represents all the possible outcomes from flipping a coin k times. Parameters ---------- int Returns ------- TreeNode """ node = TreeNode(value) if k != 0: node.left = build_coinflip_tree(k - 1, value + "H") node.right = build_coinflip_tree(k - 1, value + "T") return node
def test_basic_relation(self): parent = TreeNode(9) node = TreeNode(8) left = TreeNode(6) right = TreeNode(7) node.parent = parent node.left = left node.right = right self.assertEqual(id(node.parent), id(parent)) self.assertEqual(id(node.left), id(left)) self.assertEqual(id(node.right), id(right)) self.assertEqual(id(left.parent), id(node)) self.assertEqual(id(right.parent), id(node)) self.assertEqual(id(node.parent), id(parent)) self.assertEqual(id(parent.left), id(node)) self.assertTrue(parent.key > left.key) self.assertTrue(parent > left)
def create_by_pre_in_order(values1, values2): if not values1 or not values2: return None root_value = values1[0] i = 0 while values2[i] != root_value: i += 1 left_values1 = values1[1:i + 1] left_values2 = values2[:i] right_values1 = values1[i + 1:] right_values2 = values2[i + 1:] node = TreeNode(value=root_value) node.left = create_by_pre_in_order(left_values1, left_values2) node.right = create_by_pre_in_order(right_values1, right_values2) return node
def from_pre_post(pre: List[int], post: List[int]) -> TreeNode: if not pre: return None root = TreeNode(pre[0]) # 左右边界判定 if len(pre) == 1: return root left_val = pre[1] left_count = post.index(left_val) + 1 root.left = Construct.from_pre_post(pre[1:left_count + 1], post[0:left_count]) root.right = Construct.from_pre_post(pre[left_count + 1:], post[left_count:-1]) return root
if k == 0: return None else: return TreeNode(flips, build_coinflip_tree(k - 1, flips + 'H'), build_coinflip_tree(k - 1, flips + 'T')) # node.left = build_coinflip_tree(node, k-1) # node.right = build_coinflip_tree(node, k-1) if __name__ == '__main__': # build a tree # 1 # / \ # 2 3 # / # 4 t1 = TreeNode(1) t1.left = TreeNode(2) t1.right = TreeNode(3) t1.left.left = TreeNode(4) # build a tree # 1 # / \ # 2 3 # / / # 4 5 t2 = TreeNode(1) t2.left = TreeNode(2) t2.right = TreeNode(3) t2.left.left = TreeNode(4) t2.right.left = TreeNode(5)
else: current = stack.pop() if current.right is None or current.right == prev: visit_it(current.val) prev = current current = None else: stack.append(current) stack.append(current.right) current = current.right.left """ if __name__ == '__main__': root = TreeNode(1) root.left = TreeNode(2, right=TreeNode(3)) root.right = TreeNode(4, left=TreeNode(5)) root.right.left.left = TreeNode(6, right=TreeNode(7, right=TreeNode(8))) root.right.left.right = TreeNode(9) pre_order_array = [] RecursiveMode.pre_order(root, lambda x: pre_order_array.append(x)) assert pre_order_array == [1, 2, 3, 4, 5, 6, 7, 8, 9] in_order_array = [] RecursiveMode.in_order(root, lambda x: in_order_array.append(x)) assert in_order_array == [2, 3, 1, 6, 7, 8, 5, 9, 4] post_order_array = [] RecursiveMode.post_order(root, lambda x: post_order_array.append(x)) assert post_order_array == [3, 2, 8, 7, 6, 9, 5, 4, 1]
def get_height(node): '''Return the height of the tree.''' if not node: return 0 return 1 + max(get_height(node.left), get_height(node.right)) if __name__ == '__main__': # build a tree # 1 # / \ # 2 3 # / # 4 t1 = TreeNode(1) t1.left = TreeNode(2) t1.right = TreeNode(3) t1.left.left = TreeNode(4) # build a tree # 1 # / \ # 2 3 # / / # 4 5 t2 = TreeNode(1) t2.left = TreeNode(2) t2.right = TreeNode(3) t2.left.left = TreeNode(4) t2.right.left = TreeNode(5)
return self.result def dfs(self, root, target, currPathSum, cache): import pdb pdb.set_trace() # exit condition if root is None: return # calculate currPathSum and required oldPathSum currPathSum += root.val oldPathSum = currPathSum - target # update result and cache self.result += cache.get(oldPathSum, 0) cache[currPathSum] = cache.get(currPathSum, 0) + 1 # dfs breakdown self.dfs(root.left, target, currPathSum, cache) self.dfs(root.right, target, currPathSum, cache) # when move to a different branch, the currPathSum is no longer available, hence remove one. cache[currPathSum] -= 1 node1 = TreeNode(1) node3 = TreeNode(3) node1b = TreeNode(1) node1.left = node3 node3.left = node1b sol = Solution() sol.pathSum(node1, 4)
from node import TreeNode from dfs import Recursion, NonRecursion ''' 1 2 3 4 5 6 7 ''' root = TreeNode(1) root.left = TreeNode(2) root.right = TreeNode(3) root.left.left = TreeNode(4) root.left.right = TreeNode(5) root.right.left = TreeNode(6) root.right.right = TreeNode(7) print 'DFS Recursion' dfsRecursion = Recursion() print 'Pre Order' dfsRecursion.preOrder(root) print 'In Order' dfsRecursion.inOrder(root) print 'DFS NonRecursion' dfsNonRecursion = NonRecursion() print 'Pre Order' dfsNonRecursion.preOrder(root)
# return ans if not root: return [[]] res = [] def dfs(root: TreeNode, q: list, path: list): if root.left: q.append(root.left) if root.right: q.append(root.right) if not q: res.append(path) for i in range(len(q)): dfs(q[i], q[0:i] + q[i + 1:], path + [q[i].val]) dfs(root, [], [root.val]) return res if __name__ == '__main__': t = TreeNode(2) t.left = TreeNode(1) t.right = TreeNode(3) # [5, 2, null, 1, 4, null, null, 3] s = Solution() r = s.BSTSequences(t) print(r)