def treeToString(node, pretty=False, indent=0):
    """Return a Newick string encoding node and its descendants, optionally pretty-printing with
    newlines and indentation.  String is not ';'-terminated, caller must do that."""
    if not node:
        return ''
    labelLen = ''
    if (node['label']):
        labelLen += node['label']
    if (node['length']):
        labelLen += ':' + node['length']
    if (node['kids']):
        string = '('
        kidIndent = indent + 1
        kidStrings = [ treeToString(kid, pretty, kidIndent) for kid in node['kids'] ]
        sep = ','
        if (pretty):
            sep = ',\n' + ' ' * kidIndent
        string += sep.join(kidStrings)
        string += ')'
        string += labelLen
    else:
        string = labelLen
    return string;