Skip to content

Instantly share code, notes, and snippets.

/tree.py
Created Jul 1, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.