[Leetcode]*105.construct Binary Tree from preorder and inorder traversal

Source: Internet
Author: User

Topic

Given Preorder and inorder traversal of a tree, construct the binary tree.

Note:
Assume that duplicates does not exist in the tree.

Ideas

This topic is mainly based on the characteristics of the pre-sequence traversal and the middle sequence traversal.

1. Determine the root node of the tree. The root is the first element in the current tree in which all elements appear in the pre-order traversal.
2, to solve the subtree tree. Find out where the root node is in the middle sequence traversal, all elements on the left side of the root are the left dial hand tree, and all elements to the right of the root are right subtrees. If the left or right side of the root node is empty, the direction subtree is empty, and the root node is already a leaf node if both the root and right sides are empty.
3, recursive solution tree. Consider the Saozi right subtree as a binary tree, repeating 1, 2, and 3 steps until all nodes are positioned

Code

/* ---------------------------------------* Date: 2015-04-28* sjf0115* title: 105.Construct Binary Tree from preorder a nd inorder traversal* website: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ * Result: ac* Source: leetcode* Blog:-----------------------------------------* /#include <iostream>#include <vector>#include <algorithm>using namespace STD;structtreenode{intVal    TreeNode *left;    TreeNode *right; TreeNode (intx): Val (x), left (nullptr), Right (nullptr){}};classSolution { Public: TreeNode *buildtree ( vector<int>&preorder, vector<int>&inorder) {intSize = Preorder.size ();if(Size <=0){return nullptr; }//if        returnPreinbuildtree (Preorder,inorder,0,0, size); }Private: treenode* Preinbuildtree ( vector<int>&preorder, vector<int>&inorder,intPreindex,intInindex,intSize) {if(Size <=0){return nullptr; }//if        //root nodetreenode* root =NewTreeNode (Preorder[preindex]);//Search for the subscript of the root node in the middle sequence traversal array        intindex =0; for(inti =0; i < size;++i) {if(Preorder[preindex] = = Inorder[inindex+i]) {index = inindex+i; Break; }//if}//for        //Left dial hand tree number        intLeftsize = Index-inindex;number of//right sub-trees        intRightsize = Size-leftsize-1;//Left dial hand treeRoot->left = Preinbuildtree (preorder,inorder,preindex+1, inindex,leftsize);//Right sub-treeRoot->right = Preinbuildtree (preorder,inorder,preindex+1+leftsize,index+1, rightsize);returnRoot }};voidPostorder (treenode* root) {if(Root)        {postorder (root->left); Postorder (Root->right);cout<<root->val<<endl; }//if}intMain () {solution solution; vector<int>Preorder = {1,2,4,8,5,3,6,7}; vector<int>Inorder = {8,4,2,5,1,6,3,7}; treenode* root = Solution.buildtree (Preorder,inorder);//OutputPostorder (root);return 0;}

Run time

[Leetcode]*105.construct Binary Tree from preorder and inorder traversal

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.