The post-order traversal sequence of the Binary Search Tree (determining whether the post-order traversal sequence is valid)

Source: Internet
Author: User

(1) Description: enter an integer array to determine whether the array is a result of post-sequential traversal of a binary search tree. If Yes, Yes is output; otherwise, No is output. Assume that any two numbers in the input array are different. Input: each test case contains two rows. The first row is an integer of n (1 <= n <= 10000), indicating the length of the array. The second row contains n integers, indicating the array. The value range of the number in the array is [0,]. Output: For each test case, if the input array is the result of a forward traversal of a binary search tree, the output is Yes; otherwise, the output No (2) program implements the binary search tree. The properties are as follows: all the elements in the left subtree of an element are smaller than this element, and all the elements in the right subtree are greater than this element. The root node is always at the end of the post-order traversal. Therefore, the root element can be obtained according to the post-order traversal order, and the left and right subtree of the root element can be obtained. Assume that the descending order of the tree is 3, 7, 4, 6, and 5. You can see that 5 is the root node, 3 is the left subtree of the root node, 7, 4, 6 is the right subtree of the root node, but the right subtree must ensure that all nodes are greater than the root node. Therefore, this order is not the post-order traversal order of the Binary Search Tree. Solution: when identifying the left and right subtree, traverse the right subtree and find that the elements in the right subtree are smaller than the parent node, the order is wrong. (PS: This solution still needs to be optimized. Currently, the solution is to store two variables and identify the maximum and minimum values of elements in the subtree) [cpp] # include <iostream> using namespace std; bool IsValid (int * data, int n, int left, int right) {if (left> = right) return true; int mid = 0; int root = data [right]; // identifies the left subtree, where the right subtree is located for (mid = left; mid <right, data [mid] <root; ++ mid); // determines whether the subtree is valid for (int I = mid; I <right; ++ I) {if (data [I] <root) return false;} if (! IsValid (data, n, left, mid-1) return false; if (! IsValid (data, n, mid + 1, right) return false; return true;} int main () {int n; while (cin> n) {int * data = new int [n]; for (int I = 0; I <n; ++ I) cin> data [I]; if (! IsValid (data, n, 0, n-1) cout <"No" <endl; else cout <"Yes" <endl; delete [] data ;}}

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.