# Display List Code Below: def height(node): if (node == None): return 0 return 1 + max(height(node.left), height(node.right)) def draw_node(output, link_above, node, level, p, link_char): if (node == None): return out = "[" h = len(output) SP = " " if (p < 0): for s in output: if (s): s = " "*(-1*p) + s for s in link_above: if (s): s = " "*(-1*p) + s if level < h - 1: p = max(p, len(output[level + 1])) if (level > 0): p = max(p, len(output[level - 1])) p = max(p, len(output[level])) # Fill in to left if (node.left): leftData = SP + str(node.left.data) + SP draw_node(output, link_above, node.left, level + 1, p - len(leftData),'L') p = max(p, len(output[level + 1])) # Enter this data space = p - len(output[level]) if (space > 0): output[level] += (' ' * space) node_data = SP + str(node.data) + SP output[level] += node_data # Add vertical link above space = p + len(SP) - len(link_above[level]) if (space > 0): link_above[level] += (' ' * space) link_above[level] += link_char # Fill in to right if (node.right): draw_node(output, link_above, node.right, level + 1, len(output[level]),'R') def display_tree(root): if (root == None): print("\tNone") h = height(root) output = [] link_above = [] for i in range(0,h): output.append("") link_above.append("") draw_node(output, link_above, root, 0, 5, ' ') # Create link lines for i in range(h): for j in range(len(link_above[i])): if (link_above[i][j] != ' '): size = len(output[i - 1]) if (size < j + 1): output[i - 1] += " " *(j + 1 - size) jj = j if (link_above[i][j] == 'L'): while (output[i - 1][jj] == ' '): jj += 1 for k in range(j+1, jj-1): str1 = output[i - 1] list1 = list(str1) list1[k] = '_' output[i - 1] = ''.join(list1) elif(link_above[i][j] == 'R'): while (output[i - 1][jj] == ' '): jj-=1 # k = j-1 for k in range(j-1,jj+1,-1): temp = output[i - 1] list1 = list(temp) list1[k] = '_' output[i - 1] = ''.join(list1) k = k - 1 str1 = link_above[i] list1 = list(str1) list1[j] = '|' link_above[i] = ''.join(list1) # Output for i in range(h): if (i): print("\t" , link_above[i]) print("\t" , output[i]) from BinaryTree import * from TreeNode import * def max_contrib(root): if not root: return 0 # sum of the left and right subtree max_left = max_contrib(root.left) max_right = max_contrib(root.right) left_subtree = 0 right_subtree = 0 # max sum on the left and right sub-trees of root if max_left > 0: left_subtree = max_left if max_right > 0: right_subtree = max_right # the value to start a new path where `root` is a highest root value_new_path = root.data + left_subtree + right_subtree # update max_sum if it's better to start a new path max_sum = max_contrib.max_sum max_contrib.max_sum = max(max_sum, value_new_path) # for recursion: # return the max contribution if continue the same path return root.data + max(left_subtree, right_subtree) def max_path_sum(root): max_contrib.max_sum = float('-inf') max_contrib(root) return max_contrib.max_sum # Driver code def main(): input_trees = [ [TreeNode(-8), TreeNode(2), TreeNode(17), TreeNode(1), TreeNode(4), TreeNode(19), TreeNode(5)], [TreeNode(7), TreeNode(3), TreeNode(4), TreeNode(-1), TreeNode(-3)], [TreeNode(-10), TreeNode(9), TreeNode(20), TreeNode(30), TreeNode(16), TreeNode(15), TreeNode(7)], [TreeNode(1),TreeNode(2),TreeNode(3)], [TreeNode(0)], [TreeNode(-10), TreeNode(9), TreeNode(20), None, None, TreeNode(15), TreeNode(7)], [TreeNode(1), TreeNode(-3),TreeNode(3), TreeNode(5), None, None, TreeNode(-5)] ] indx = 1 for i in input_trees: tree = BinaryTree(i) print(indx, ".\tBinary Tree:", sep="") indx += 1 display_tree(tree.root) print("\n\t Maximum path sum:\t", max_path_sum(tree.root)) print("\n", "-"*100) if __name__ == '__main__': main()