class SymbolTable: def __init__(self): self.__binaryTree = Tree() def add(self, value): return self.__binaryTree.add(value) def __str__(self): return str(self.__binaryTree)
from BinaryTree import Tree """ BinaryTree.py has been defined """ p = Tree() q = Tree() test1 = [1, None, 2] test2 = [1, 2] p.add(test1) q.add(test2) p_node = p.root q_node = q.root def isSameTree(p, q): if p is None and q is None: return True if p is None or q is None: return False if p.item == q.item: return isSameTree(p.left, q.left) and isSameTree(p.right, q.right) else: return False if isSameTree(p_node, q_node): print('True') else: print('False')
sum -= u.item # means we have reach node u if sum == 0 and u.left == None and u.right == None: # if node u is leave_node and subSum is 0 return True # reach node u, next to check U_child # regard u_child as new root elif u.left and zp_dfs(u.left, sum): return True elif u.right and zp_dfs(u.right, sum): return True else: return False def hasPathSum(root, sum): if root == None: # no path return False else: return zp_dfs(root, sum) sum = 22 q = Tree() test1 = [5, 4, 8, 11, None, 13, 4, 7, 2, None, None, None, 1] q.add(test1) print(q.preorder(q.root)) q_node = q.root if hasPathSum(q_node, sum): print('True') else: print('False')