23. 二进制搜索树(python)的后遍历
标题说明
输入一个整数数组二叉排序树的遍历,以确定该数组是否是遍历二叉搜索树的结果. 如果是,则输出“是”二叉排序树的遍历,否则输出“否”. 假设输入数组中的任何两个数字互不相同.
这个问题特别愚蠢:
输入序列为空时返回
false,但在递归过程中为true
1 # -*- coding:utf-8 -*- 2 class Solution: 3 def VerifySquenceOfBST(self, sequence): 4 # write code here 5 if sequence==[]: 6 return False 7 rootNum=sequence[-1] 8 del sequence[-1] 9 index=None 10 for i in range(len(sequence)): 11 if index == None and sequence[i]>rootNum: 12 index = i 13 if index !=None and sequence[i]< rootNum: 14 return False 15 if sequence[:index]==[]: 16 left = True 17 else: 18 left = self.VerifySquenceOfBST(sequence[:index]) 19 if sequence[index:]==[]: 20 right = True 21 else: 22 right = self.VerifySquenceOfBST(sequence[index:]) 23 return left and right
2019-12-1208: 56: 33
发布于2019-12-12 08:58阿桑奇阅读(...)评论(...)编辑
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-241254-1.html
@LINX11mv的意思表达了她们的心情与决心欢迎归来
期待你的小王子