Sequential traversal sequence of binary search tree

Source: Internet
Author: User

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

Describe:
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

Binary search tree is also called binary sorting tree, that is, the left child's values are all small root node, the right child's value is all greater than the root node, the root of the child is also a binary search tree.
Given the following binary search tree:

The subsequent sequence traversal is: 2 9 5 16 17 15 19 18 12.
Now assume that a given post-order traversal array is: 2 9 5 16 17 15 19 18 12
We start from the root node, that is, node 12, if this array is the result of a binary sort tree follow-up, it is said that the data in the array must be divided into two parts: part is all less than 12, part of the whole is greater than 12, we first from the array to find the first number greater than 12 is 16, then the array can
First part: 2 9 5
Part II: 16 17 15 19 18
To make the root node 12 satisfy the definition of the binary sort tree, the first part must be all less than 12, and the second part is all greater than 12.
At this point, the first part and the second part of the root node 12 of the child to meet the situation of the binary search tree, the first part of the subtree of the root node is 5, the second part of the subtree represents the root node is 18, that is, we now want to determine whether the root node is 5 and the root node 18 of the two trees are satisfied This is a recursive process.

Class listnode{intVal ListNode next=NULL; ListNode (intVal) { This. Val=val; }    } Public int findposition(int[] sequence,intStartintEndintVal) {//Find the first position greater than Val from sequence's Start-end        intI=start; for(; i<=end;i++) {if(Sequence[i]>val) Break; }returnI } Public Boolean Issquenceofbst(int[] sequence,intStartintEnd) {if(end<0|| Start>=sequence.length)return true;intVal=sequence[end];intposition=0;if(Start<end) {//The array is divided into two parts in position, the left part is necessarily all less than Val, the right part must be all greater than ValPosition=findposition (sequence, start, end-1, Val); for(inti=position;i<=end;i++) {if(Sequence[i]<val)//If there is a number less than Val on the right part does not conform to the definition of binary search tree                    return false; }        }Else{return true; }//Determine whether the left and right sub-trees meet the definition of binary search tree        returnIssquenceofbst (sequence,start,position-1) && Issquenceofbst (sequence, position, end-1); }/** * Enter an array of integers to determine if the array is the result of a sequential traversal of a binary search tree * * /     Public Boolean Verifysquenceofbst(int[] sequence) {if(sequence.length<=0)return false;returnISSQUENCEOFBST (Sequence,0, sequence.length-1); }

Sequential traversal sequence of 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.