コード例 #1
0
def create_lists_3_helper(r, lists, level):
    if not r:
        return
    # if the level is not visited before, create a new list
    if level>len(lists)-1:
        current_list=LinkList()
        current_list.append_node(r)
        lists.append(current_list)
    # else, get the list, append the tree node
    else:
        current_list=lists[level]
        current_list.append_node(r)
        
    create_lists_3_helper(r.left, lists, level+1)
    create_lists_3_helper(r.right, lists, level+1)
コード例 #2
0
def create_lists_1(r):
    q=deque([])
    level=deque([])
    lists=[]
    q.append(r)
    level.append(0)
    level_list=LinkList()
    level_list.append_node(r)

    while q:
        current_node=q.popleft()
        current_level=level.popleft()
        if current_node.left:
            q.append(current_node.left)
            level.append(current_level+1)
        if current_node.right:
            q.append(current_node.right)
            level.append(current_level+1)
        
        if q:
            # if this node is at the same level of the previous node, then add this node to the list
            if level[0]==current_level:
                level_list.append_node(q[0])
            # else, then pack the list to lists, create a new list, and add this node to the list
            else:
                lists.append(level_list)
                level_list=LinkList()
                level_list.append_node(q[0])
        # if it's the last element, pack to lists
        else:
            lists.append(level_list)
    return lists
コード例 #3
0
def create_lists_2(r):
    lists=[]
    current=LinkList()
    if r:
        current.append_node(r)
    while len(current)>0:
        lists.append(current)       # pack to lists
        parents=current     # go one level down
        current=LinkList()  # create a new list
        for parent in parents:
            if parent.left:
                current.append_node(parent.left)
            if parent.right:
                current.append_node(parent.right)
    return lists