对于一颗满二叉排序树深度为K,求最小子树根节点值 Python代码实现
def binary_tree():
n = int(input("请输入层数:"))
a = int(input("清输入三个节点数:"))
b = int(input())
c = int(input())
ma = max(a, b, c)
mi = min(a, b, c)
left = 1
right = pow(2, n) - 1
middle = (left + right) / 2
while True:
if(middle > mi) and (middle < ma):
break
if(middle < mi):
left = middle + 1
middle = (left + right) / 2
elif(middle > ma):
right = middle -1
middle = (left + right) / 2
print(int(middle))
一颗满二叉排序树(节点数为2^k – 1,节点值为1至(2^k – 1)),就是一个采用中序遍历结果为自然数顺序的满二叉树,比如,一个3层的满二叉排序树,中序遍历的结果就是1,2,3,4,5,6,7.