1 二叉查找树填空(10分)
题目内容:
给定一个二叉树结构,与一个整数列表,请将整数填充至二叉树对应节点内,使其成为一个二叉查找树;请输出该二叉查找树的层次遍历。下图展示了给定样例对应的二叉树结构:
输入格式:
每个测试样例第一行包含一个整数,为二叉树的节点总数N。随后N行分别给定了编号由0至(N-1)的节点的左右子树编号,以空格分隔;若编号-1则代表对应子树为空。最后一行给出了以空格分隔的N个整数
输出格式:
对填空后的二叉查找树进行层次遍历,按顺序输出整数序列,即从第1层根结点开始,逐层向下,同一层从左到右,以空格分隔,行尾无多余空格
输入样例:
9
1 6
2 3
-1 -1
-1 4
5 -1
-1 -1
7 -1
-1 8
-1 -1
73 45 11 58 82 25 67 38 42
输出样例:
58 25 82 11 38 67 45 73 42
解题思路:
先按照题目要求构造二叉树:先创建N个树节点添加到列表里,然后根据每个节点的左右子树编号将各节点链接起来。接着中序遍历二叉树将整数序列按从小到大的顺序放入二叉树中,最后逐层输出
程序代码:
class TreeNode:
def __init__(self, key, left=None, right=None, parent=None):
self.key = key
self.val = None
self.leftChild = left
self.rightChild = right
def generateTree(total):
node_list = [TreeNode(i) for i in range(total)]
for currentNode in node_list:
seq = [int(j) for j in input().split()]
if seq[0] != -1:
currentNode.leftChild = node_list[seq[0]]
if seq[1] != -1:
currentNode.rightChild = node_list[seq[1]]
return node_list[0]
def fillTree(root):
if root:
fillTree(root.leftChild)
root.val = lst.pop(0)
fillTree(root.rightChild)
def printTree(total):
queue = [root]
result = []
for i in range(total):
currentNode = queue.pop(0)
result.append(currentNode.val)
if currentNode.leftChild:
queue.append(currentNode.leftChild)
if currentNode.rightChild:
queue.append(currentNode.rightChild)
print(*result)
N = int(input())
root = generateTree(N)
lst = [int(i) for i in input().split()]
lst.sort()
fillTree(root)
printTree(N)
2 完全二叉查找树(10分)
题目内容:
给定一系列整数,请构造相应的二叉树,使其既是二叉查找树又是完全二叉树;请输出满足条件的二叉树的层次遍历。
输入格式:
一个整数序列,以空格分隔
输出格式:
对应完全二叉查找树的层次遍历序列,即从第1层根结点开始,逐层向下,同一层从左到右,以空格分隔,行末无多余空格
输入样例:
1 2 3 4 5 6 7 8 9 0
输出样例:
6 3 8 1 5 7 9 0 2 4
解题思路:
先构造一个完全二叉树,然后如前一题一样中序遍历二叉树同时将整数序列依次存入节点中
程序代码:
class TreeNode:
def __init__(self, key, left=None, right=None, parent=None):
self.key = key
self.val = None
self.leftChild = left
self.rightChild = right
self.parent = parent
def generateTree(total):
root = TreeNode(1)
queue = [root]
count = 1
while count < total:
currentNode = queue.pop(0)
if count + 1 < total:
count += 1
currentNode.leftChild = TreeNode(count, parent=currentNode)
queue.append(currentNode.leftChild)
count += 1
currentNode.rightChild = TreeNode(count, parent=currentNode)
queue.append(currentNode.rightChild)
else:
count += 1
currentNode.leftChild = TreeNode(count, parent=currentNode)
return root
def fillTree(root):
if root:
fillTree(root.leftChild)
root.val = lst.pop(0)
fillTree(root.rightChild)
def printTree(total):
queue = [root]
result = []
for i in range(total):
currentNode = queue.pop(0)
result.append(currentNode.val)
if currentNode.leftChild:
queue.append(currentNode.leftChild)
if currentNode.rightChild:
queue.append(currentNode.rightChild)
print(*result)
lst = [int(num) for num in input().split()]
lst.sort()
N = len(lst)
root = generateTree(N)
fillTree(root)
printTree(N)
3 从二叉搜索树到更大和树(10分)
题目内容:
给定一个二叉搜索树,请修改树节点,使新树中每个节点的值等于原树中大于等于该节点的值之和;请输出修改后的树的层次遍历序列。
输入格式:
一个不重复的整数序列,以空格分隔,为构造原二叉查找树的节点插入顺序
注:题目保证输入序列无重复
输出格式:
修改后的树的层次遍历序列,即从第1层根结点开始,逐层向下,同一层从左到右,以空格分隔,行尾无多余空格
输入样例:
6 1 8 3 4 9 2 7 5 0
输出样例:
30 45 17 45 42 24 9 44 39 35
解题思路:
先将整数序列依次存入节点中,接着逐层将节点的值与序列中的每个数进行对比,将大于等于节点值的数加起来放在列表中,最后打印列表
程序代码:
class TreeNode:
def __init__(self, key, left=None, right=None, parent=None):
self.key = key
self.val = None
self.leftChild = left
self.rightChild = right
self.parent = parent
def put(self, key):
if key < self.key:
if self.leftChild:
self.leftChild.put(key)
else:
self.leftChild = TreeNode(key, parent=self)
else:
if self.rightChild:
self.rightChild.put(key)
else:
self.rightChild = TreeNode(key, parent=self)
def generateTree():
root = TreeNode(lst[0])
for i in lst[1:]:
root.put(i)
return root
def changeTree():
queue = [root]
result = []
for i in range(len(lst)):
currentNode = queue.pop(0)
total = 0
for num in lst:
if num >= currentNode.key:
total += num
result.append(total)
if currentNode.leftChild:
queue.append(currentNode.leftChild)
if currentNode.rightChild:
queue.append(currentNode.rightChild)
print(*result)
lst = [int(num) for num in input().split()]
root = generateTree()
changeTree()