The sword refers to offer 23. Sequential traversal sequence of binary search tree (binary search tree)

Source: Internet
Author: User

Title Description

Enter an array of integers to determine if the array is the result of a sequential traversal of a binary search tree. If yes, output yes, otherwise output No. Assume that any two digits of the input array are different.

Title Address

https://www.nowcoder.com/practice/a861533d45854474ac791d90e447bafd?tpId=13&tqId=11176&rp=2&ru=/ta/ Coding-interviews&qru=/ta/coding-interviews/question-ranking

Ideas

Ideas:

Take [5,7,6,9,11,10,8] as an example, the last digit of the post-order traversal result 8 is the value of the root node, in which the first three digits 5, 7, and 6 are smaller than 8, the left subtree node of the node with a value of 8, and the next three digits 9, 11, and 10 are larger than 8, which is the right subtree of the node with a value of 8.

Using the same method to determine the structure of the sub-tree corresponding to each part of the array, this is actually a recursive process, for the sequence 5,7,6, the last number 6 is the Zuozi root node value, the number 5:6 is small, is a value of 6 node left Dial hand node, and 7 is its right child node. Similarly, in sequences 9, 11, 10, the last Number 10 is the root node of the right subtree, the number 9:10 is small, is the node with a value of 10 left Dial hand node, and 11 is its right child node.

Using the recursive method, the Saozi of the right subtree of the array is determined first, and the left subtree is located. Determine if the right subtree is a binary search tree.

Python

#-*-coding:utf-8-*-classSolution:defVerifysquenceofbst (self, sequence):#Write code here        ifLen (sequence) = =0:returnFalseifLen (sequence) = = 1:            returnTrue Root= Sequence[-1]#Get root nodei =0 whileSequence[i] < root:#left dial hand treei + = 1k=I forIinchRange (K,len (sequence)):ifSequence[i] <= Root:#Judging right sub-tree                returnFalse Left_k=Sequence[:k] Right_k= Sequence[k:len (Sequence)-1] left, right=true, TrueifLen (Left_k) >0:left=Self . Verifysquenceofbst (Left_k)ifLen (Right_k) >0:right=Self . Verifysquenceofbst (Right_k)returnLeft and Rightif __name__=='__main__': Sequence= [4,6,5,9,11,10,8] Result=solution (). VERIFYSQUENCEOFBST (Sequence)Print(Result)

The sword refers to offer 23. Sequential traversal sequence of binary search tree (binary search tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.