# Q：找到一个二进制字符串的字符串最长树蟒

Alright so I need to define a recursive function longest_length() that takes a binary string tree and returns the length of the longest string in the tree.

I admittedly have no clue how to do this, but this is what I have set up:

``````def add_leaves(bnt):
"""Takes a BNT and returns the length of the largest string in the tree.

BNT - number"""
if isinstance(bnt, str):
return bnt
else:
return ????
``````

can anyone help me out? this isn't really homework, just a question I came upon while studying for finals that I feel I should know how to answer

``````def add_leaves(bnt):
"""Takes a BNT and returns the length of the largest string in the tree.

BNT - number"""
if isinstance(bnt, str):
return bnt
else:
return ????
``````

Assuming you have a tree node class that has a string value attribute, and attributes to store its left and right children, the implementation would be fairly simple:

``````class TreeNode:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right

def find_max(node):
if (node is None): return ""
return max([find_max(node.left), node.val, find_max(node.right)], key=len)

tree = TreeNode("test", TreeNode("asdasjkdnaskdjasd", TreeNode("")), TreeNode("a"))
``````

You can see a demo here: http://ideone.com/SE39tM

``````class TreeNode:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right

def find_max(node):
if (node is None): return ""
return max([find_max(node.left), node.val, find_max(node.right)], key=len)

tree = TreeNode("test", TreeNode("asdasjkdnaskdjasd", TreeNode("")), TreeNode("a"))