Skip to content

Instantly share code, notes, and snippets.

Created July 1, 2015 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/ea3764fb618ce8ff66aa to your computer and use it in GitHub Desktop.
Save anonymous/ea3764fb618ce8ff66aa to your computer and use it in GitHub Desktop.
class Node(object):
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
def max_of_tree(root):
if not root:
return 0
max_left_path = max_of_tree(root.left)
max_right_path = max_of_tree(root.right)
return max(max_left_path, max_right_path) + root.val
node_20 = Node(54)
node_21 = Node(55)
node_23 = Node(3)
node_10 = Node(1, node_20, node_21)
node_11 = Node(2, None, node_23)
root = Node(1, node_10, node_11)
print max_of_tree(root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment