Skip to content

Instantly share code, notes, and snippets.

@SakuraSa SakuraSa/tree_width.py
Last active Oct 20, 2015

Embed
What would you like to do?
get tree width
class Node:
left = right = None
def node_only(iterable_nodes):
return filter(bool, iterable_nodes)
def iter_layer(layer):
layer = node_only(layer)
while layer:
yield layer
layer = node_only(node.left for node in layer) + node_only(node.right for node in layer)
def tree_width(root):
return max(map(len, iter_layer([root])) or [0])
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.