Determine whether sequential traversal of ordered binary trees is correct (summary of recursive calculation)

Source: Internet
Author: User

Is
#include <iostream>using namespace The nature of the binary tree of std;//*/*. Termination condition: 1. Start >= End returns True *. 2.S >= e Because there is no problem, it will be able to reach the s>=e situation.     Knowing the arrival of S >= E is true *.  1. The last node is root 2. The node that precedes root is its right subtree, which is more than root, and the left subtree is 3. Recursive call */bool Treehelper (int a[], int s, int e) {if (A = = NULL) return 0;if (S >= e) return 1;  In the absence of a right subtree, it may appear that s >= e int i = E-1;while (i >= s && a[e] <= a[i]) i--;if (!treehelper (a,i+1,e-1)) {return 0;} int k = I;while (i >= s && a[e] > A[i]) i--;if (S < i+1) return 0;//If the left dial hand tree is judged to be greater than the root value, it is not a sequential r of a sort binary tree. Eturn Treehelper (A,s, k);} int postorderresult (int a[], int n) {return treehelper (a,0,n-1);} int main () {int a[] = {1,5,7,6,9,11,10,8};int i = Postorderresult (a,sizeof (a)/sizeof (a[0])); cout <<i << std:: Endl;return 0;} As for the pre-order of the more simple traversal on the line. Middle order words bool Treehelper2 (int a[], int s, int e) {if (a = = NULL) return null;if (S >= e) return 1;int i = s+1;while (i <= E && A[s] > A[i]) i++;if (!treehelper2 (a,s+1,i-1)) {return 0;} INT k = i;while (i <= e && a[s] <= a[i]) i++;if (E > I-1) return 0;return treehelper2 (a,k,e);  }

The recursion here is very classic, I summed it up, but the summary of the point ...

1. Sub-problem

2. Termination conditions (including some borders)

Sub-problem: Each post-judgment is divided into its left sub-tree of the next right sub-tree of the post-sequential, and then left to the sub-tree can be handed down. This will find a sub-problem.

Termination conditions: When s>=e, there will be s>=e only the recursive to each child node can occur, when recursive to this, this post-order represents that there is no problem.

General form of Summary:

void func (int a, int b)//The parameters here are parameters that control the size of the sub-problem

{

if (...) return; //recursive termination conditions

The following is to solve the current problem, generally involves sub-issues

int i = func (a+x,b-x); This is the division of the sub-problem, but also the control sub-problem

Int J = func (a-x,a+x); //

return i*j; //finally returns the result of this whole problem based on the value returned by the child problem.

}

Entertain yourself, light spray!!


Determine whether sequential traversal of ordered binary trees is correct (summary of recursive calculation)

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.