zoukankan      html  css  js  c++  java
  • 23.二叉搜索树的后序遍历(python)

      题目描述

    输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
    这道题特别傻的地方:
      当输入sequence为空时返回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-12 08:56:33

  • 相关阅读:
    ubuntu
    eclipse 链接 hadoop
    javaweb访问hdfs的一些错误
    eclipse
    ubuntu
    ubuntu
    ubuntu
    ubuntu 安装 mysql
    光源选择
    C#脱离Halcon编程开发环境使用方法
  • 原文地址:https://www.cnblogs.com/NPC-assange/p/12026957.html
Copyright © 2011-2022 走看看