Topic:
Given Preorder and inorder traversal of a tree, construct the binary tree.
Note:
Assume that duplicates does not exist in the tree.
Constructs a two-fork tree based on the pre-order traversal and the middle sequence traversal results.
Thinking Analysis:
Analyzing the results of binary tree pre-sequence traversal and middle sequence traversal we found:
The first node of a binary tree pre-sequence traversal is the root node.
Find the root node in the middle sequence traversal, separated by the root node, the middle sequence traverse to the left is the record sequence traversal result (with X nodes), right is the result (with Y nodes).
The pre-sequence traversal removes the first root node, the number of X nodes, the x nodes are the result of the left subtree's pre-order traversal, and the remaining nodes (which are definitely y) are the results of the right subtree pre-order traversal.
In this way we get the results of the pre-sequence traversal and the middle sequence traversal of the left and right sub-trees, so we return to the original problem, so that we naturally think of recursion.
C + + code:
/** * Definition for binary tree * struct TreeNode {* int val; * TreeNode *left; * TreeNode *right; * TreeNode (int x): Val (x), left (null), right (NULL) {} *}; */classsolution{Private: TreeNode *makenode ( vector<int>:: Iterator Prebegin, vector<int>:: Iterator Preend, vector<int>:: Iterator Inbegin, vector<int>:: Iterator Inend) {if(Prebegin = = preend)return nullptr;The root node (the root node is the first node in the pre-sequence traversal)///from the sequence traversal results vector<int>:: Iterator Itroot = Find (Inbegin, inend, *prebegin); TreeNode *root =NewTreeNode (*itroot);//Calculate the number of left subtree nodes of the root intLeftsize = Itroot-inbegin;The left side of the root node in the middle sequence traversal result is the record sequence traversal result, right is the right subtree in the sequence traversal result ///before removing the root node in the pre-order traversal results, the Leftsize node is the left subtree pre-sequence traversal result, and the following node is the right subtree pre-order traversal resultRoot->left = Makenode (Prebegin +1, Prebegin + leftsize +1, Inbegin, Itroot); Root->right = Makenode (prebegin + leftsize +1, Preend, Itroot +1, inend);returnRoot } Public: TreeNode *buildtree ( vector<int>&preorder, vector<int>&inorder) {if(Preorder.empty ())return nullptr; TreeNode *root = Makenode (Preorder.begin (), Preorder.end (), Inorder.begin (), Inorder.end ());returnRoot }};
Where the Find function is signed as follows:
Template <class Inputiterator, class t> Inputiterator Find (inputiterator First, Inputiterator last, const T&AM P Val); Find value in Rangereturns a iterator to the first element in the range [First,last] that compares equal to Val. If no such element is found, the function returns last. The function uses operator== to compare the individual elements to Val.
The vector::iterator in the preceding code is lengthy, and the way we use C + + templates can be a little easier to do (of course, the C + + template is not for this purpose). In addition, you can use typedef for simplification of the notation.
classsolution{Private:Template<TypeNameT> TreeNode *makenode (t prebegin, T preend, T inbegin, T inend) {if(Prebegin = = preend)return nullptr;AutoItroot = Find (Inbegin, inend, *prebegin); TreeNode *root =NewTreeNode (*itroot);intLeftsize = Itroot-inbegin; Root->left = Makenode (Prebegin +1, Prebegin + leftsize +1, Inbegin, Itroot); Root->right = Makenode (prebegin + leftsize +1, Preend, Itroot +1, inend);returnRoot } Public: TreeNode *buildtree ( vector<int>&preorder, vector<int>&inorder) {if(Preorder.empty ())return nullptr; TreeNode *root = Makenode (Preorder.begin (), Preorder.end (), Inorder.begin (), Inorder.end ());returnRoot }};
Java Reference Code:
The idea is the same as above, but Java chooses an element from the array that needs to be traversed (or it can be turned into a list or set, but with the most efficient traversal) the Find function is used in the C + + code above.
Sometimes I feel the C + + code is very concise!
/** * Definition for Binary tree * public class TreeNode {* int val, * TreeNode left, * TreeNode right; TreeNode (int x) {val = x;} *} */ Public class solution { PrivateTreeNodeMakenode(int[] Preorder,intPrebegin,intPreend,int[] inorder,intInbegin,intInend) {if(Prebegin = = preend)return NULL;intindex =0; for(inti = Inbegin; i < inend; i++) {if(Inorder[i] = = Preorder[prebegin]) {index = i; } }intLeftsize = Index-inbegin; TreeNode root =NewTreeNode (Preorder[prebegin]); Root.left = Makenode (preorder, Prebegin +1, Prebegin + leftsize +1, Inorder, Inbegin, Inbegin + leftsize); Root.right = Makenode (preorder, Prebegin + leftsize +1, Preend, inorder, index +1, inend);returnRoot } PublicTreeNodeBuildtree(int[] Preorder,int[] inorder) {if(Preorder.length = =0)return NULL; TreeNode root = Makenode (preorder,0, Preorder.length, Inorder,0, inorder.length);;returnRoot }}
Leetcode:construct Binary Tree from preorder and inorder traversal