《数据结构与算法分析python版》第九周编程作业

《数据结构与算法分析python版》第九周编程作业

1、二叉树复原

题目内容:
给定一种序列化二叉树的方式:从根节点起始按层次遍历二叉树所有“可能”存在节点的位置:若该位置存在节点,则输出节点值,并在下一层相应增加两个可用位置;否则输出None,且不增加下一层的可用位置。

例如"[5, 4, 7, 3, None, 2, None, -1, None, 9]"是下图所示的二叉树序列化的结果:

《数据结构与算法分析python版》第九周编程作业_第1张图片

其中红色箭头对所有的None进行了标记。

现给出一个二叉树以这种形式序列化的结果,请复原该二叉树并给出它的中序遍历。
输入格式:
一行合法的Python表达式,可解析为包含整数与None的列表
输出格式:
二叉树中序遍历的整数序列,以空格分隔

若无对应下标,则输出"none"
输入样例
[5, 4, 7, 3, None, 2, None, -1, None, 9]

输出样例:
-1 3 4 5 9 2 7

没得思路,转一下别人的代码
脚本

class BinaryTree:
    def __init__(self,data):
        self.data = data
        self.leftChild = None
        self.rightChild = None
        self.leftflag = 0
        self.rightflag = 0
        
def seq2tree(seq):
    queue = []
    i = 1
    currentTree = BinaryTree(seq[0])
    wholeTree = currentTree
    queue.append(currentTree)
    
    while i < len(seq) and len(queue)> 0:
        currentTree = queue.pop(0)
        if seq[i] == None:
            if currentTree.leftflag == 0 and currentTree.rightflag == 0:
                currentTree.leftflag = 1
                queue.insert(0, currentTree)
            elif currentTree.leftflag == 1 and currentTree.rightflag == 0:
                currentTree.rightflag = 1
            i = i + 1

        else:
            if currentTree.leftflag == 0 and currentTree.rightflag == 0:
                currentTree.leftChild = BinaryTree(seq[i])
                currentTree.leftflag = 1
                queue.append(currentTree.leftChild)
                queue.insert(0,currentTree)               
                i = i +1
                
            elif currentTree.leftflag == 1 and currentTree.rightflag == 0:
                currentTree.rightChild = BinaryTree(seq[i])
                currentTree.rightflag = 1
                queue.append(currentTree.rightChild)
                i = i +1
            else:
                i = i
    return wholeTree
                
def inorderTree(root):
    if root == None:
        return []
    else:
        left = inorderTree(root.leftChild)
        data = root.data
        right = inorderTree(root.rightChild)
        return left + [data] + right
            
        
lst = eval(input())
tree = seq2tree(lst)
inorder = inorderTree(tree)
print(' '.join(str(x) for x in inorder))

2、翻转二叉树

题目内容:
给定一个二叉树,请给出它的镜面翻转。

为方便起见,本题只给出完全二叉树的层次遍历,请给出相应的翻转二叉树的中序遍历。
输入格式:
一行空格分隔的整数序列,表示一个完全二叉树的层次遍历
输出格式:
一行空格分隔的整数序列,表示翻转后的二叉树的中序遍历
输入样例
4 2 7 1 3 6 9
输出样例:
9 7 6 4 3 2 1
脚本

这道题接着上面的就容易多了

class BinaryTree:
    def __init__(self,data):
        self.data = data
        self.leftChild = None
        self.rightChild = None
        self.leftflag = 0
        self.rightflag = 0
    
def seq2tree(seq):
    queue = []
    i = 1
    currentTree = BinaryTree(seq[0])
    wholeTree = currentTree
    queue.append(currentTree)
    
    while i < len(seq) and len(queue)> 0:
        currentTree = queue.pop(0)

        if currentTree.leftflag == 0 and currentTree.rightflag == 0:
            currentTree.leftChild = BinaryTree(seq[i])
            currentTree.leftflag = 1
            queue.append(currentTree.leftChild)
            queue.insert(0,currentTree)               
            i = i +1
            
        elif currentTree.leftflag == 1 and currentTree.rightflag == 0:
            currentTree.rightChild = BinaryTree(seq[i])
            currentTree.rightflag = 1
            queue.append(currentTree.rightChild)
            i = i +1
        else:
            i = i
    return wholeTree

def invertTree(wholeTree):
    if not wholeTree:
        return []
    wholeTree.leftChild, wholeTree.rightChild = wholeTree.rightChild, wholeTree.leftChild
    invertTree(wholeTree.leftChild)
    invertTree(wholeTree.rightChild)
    return wholeTree
                
def inorderTree(root):
    if root == None:
        return []
    else:
        left = inorderTree(root.leftChild)
        data = root.data
        right = inorderTree(root.rightChild)
        return left + [data] + right
            
        
lst = list(input().split(" "))
tree = seq2tree(lst)
invertedTree = invertTree(tree)
inorder = inorderTree(invertedTree)

print(' '.join(str(x) for x in inorder))

3、多叉树遍历

题目内容:
给定以嵌套列表形式给出的多叉树,求它的后序遍历

注:每个代表非空多叉树的列表包含至少一项;列表第一项代表节点值,其后每一项分别为子树;遍历子树时以列表下标从小到大的顺序进行
输入格式:
一行合法的Python表达式,可解析为嵌套列表形式的多叉树结构
输出格式:
一行整数,以空格分隔
输入样例
[1,[2,[3,[4],[5]],[6]],[7],[8,[9],[10]]]
输出样例:
4 5 3 6 2 7 9 10 8 1
脚本
《数据结构与算法分析python版》第九周编程作业_第2张图片

代码

def postorderTree(tree):
    if len(tree) == 1:
        return tree
    else:
        result = []
        for i in range(1, len(tree)):
            result += postorderTree(tree[i])
        result += [tree[0]]
        return result


lst = eval(input())
postorder = postorderTree(lst)
print(*postorder)

你可能感兴趣的:(《数据结构与算法分析python版》第九周编程作业)