Skip to content

Instantly share code, notes, and snippets.

@prongs
Created May 16, 2015 18:39
Show Gist options
  • Save prongs/fdcbc6c002a2b5822155 to your computer and use it in GitHub Desktop.
Save prongs/fdcbc6c002a2b5822155 to your computer and use it in GitHub Desktop.
class Node:
Node left
Node right
int data
int prime_depth # extra space
int path_size # extra space
global max_path_size = -1
global max_path_node = None
def calculate_max_path_node(root):
calculate_max_path_node(root.left)
calculate_max_path_node(root.right)
if data is not prime:
root.prime_depth = 0
root.path_size = 0
else:
root.prime_depth = 1 + max(root.left.prime_depth, root.right.prime_depth)
root.path_size = 1 + root.left.prime_depth + root.right.prime_depth
if root.path_size > max_path_size:
max_path_size = root.path_size
max_path_node = root
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment