分层遍历二叉树


def level_traverse_tree(node: TreeNode):
    ret = []
    if node is not None:
        cur = [node]
        while cur:
            tmp = []
            next_cur = []
            for item in cur:
                if item:
                    tmp.append(item.value)
                    next_cur.extend([item.l_node, item.r_node])
            if tmp:
                ret.append(tmp)
            cur = next_cur
    return ret


posted @ 2019-11-06 13:48  两只老虎111  阅读(127)  评论(0编辑  收藏  举报