Sicily: Calculating the height of a binary lookup tree

Source: Internet
Author: User

Description

Given a binary lookup tree, it is required to calculate its height, and each binary lookup tree will give a traversal of the first order and the middle order.

For example: A binary lookup tree its first sequence traversal is: 16, 10, 4, 15, 23; The middle sequence traversal is 4, 10, 15, 16, 23, then its height is 2 (assuming the empty tree height is-1, only the root node has a height of 0)

Input

The first line is the number of test cases. For each test case, the preorder of keys, the second line is the inorder of the keys.

The first line enters the number of test cases.

For each test case, the first line is the ordinal traversal of the key value, and the second line is the middle order traversal of the key value

Output

For each test case, use a row to output the height of the tree

Sample input Copy sample input to Clipboard
24 5 64 5 66 4 8 9 104 6 8 9 10
Sample Output
23

#include <iostream> #include <vector> #include <sstream>using namespace std;struct treenode{int data; Treenode* left; treenode* right; TreeNode (char data):d ATA (data), left (null), right (null) {}}; treenode* Buildtree (vector<int>& preorder, vector<int>& inorder) {if (Preorder.empty ()) return   NULL;   treenode* root = new TreeNode (preorder[0]);   int pos = 0;   while (Inorder[pos]! = preorder[0]) pos++;   Vector<int> Left_pre (Preorder.begin () +1, Preorder.begin () +pos+1);   Vector<int> Right_pre (Preorder.begin () +pos+1, Preorder.end ());   Vector<int> left_in (Inorder.begin (), Inorder.begin () +pos);   Vector<int> right_in (Inorder.begin () +pos+1,inorder.end ());   Root->left = Buildtree (Left_pre, left_in);   Root->right = Buildtree (Right_pre, right_in); return root; }int h (treenode* root) {if (!root) Return-1;return Max (H (root->left), H (root->right)) + 1;} void Extract (vector<int>& V, string& s) {StringStream SS (s); inT Data;while (ss >> data) V.push_back (data);}   int main () {int numtest;   Cin >> Numtest;   Cin.get ();    while (numtest--) {vector<int> preorder; vector<int> inorder;    string pre, in;    Getline (CIN, pre);     Getline (Cin, in);    Extract (Preorder,pre); Extract (Inorder,in); treenode* root = NULL; Root = Buildtree (preorder, inorder);   cout << H (Root) << Endl; } return 0;  }

  

Sicily: Calculating the height of a binary lookup 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.