Reference from: Two fork tree pre-order, middle sequence, post-order traversal Mutual seeking method (Fancheng)
Today we are still concerned about the foundation of the two-fork tree, the relationship between the pre-order, the middle order and the sequential traversal.
First, the known pre-order, the middle sequence traversal, seeking post-order.
To illustrate:
Pre-sequence traversal: Gdafemhz
Middle Sequence Traversal: ADEFGHMZ
The drawing tree seeking method:
The first step, according to the characteristics of the pre-sequence traversal, we know that the root node is G;
The second step is to observe the sequence traversal adefghmz. The adef on the left side of the root node is necessarily the left subtree of root, and the hmz on the right of G must be the right subtree of root;
The third step, observe the left subtree adef, Zuozi root node is necessarily the root of the tree leftchild. In the pre-sequence traversal, the root leftchild of the tree is located after root, so the root node of the Zuozi is D;
The fourth step, the same truth, root in the right subtree node HMZ root node can also be obtained through the pre-sequence traversal. In the pre-sequence traversal, the right subtree must be traversed before all the left subtree nodes of root and root are traversed, and the first node of the right subtree is the root node of the right subtree. It is known that the root node of the right sub-tree is m;
The fifth step, observed that the above process is recursive. First find the root node of the current tree, then divide it into left dial hand tree, right subtree, and then go to the left subtree to repeat the above procedure, and then go to the right subtree to repeat the above procedure. Finally, you can restore a tree.
The process of post-order traversal by the direct recursion of the pre-sequence and the middle order traversal can be succinctly expressed as follows:
1, determine the root, determine the left sub-tree, determine the right subtree.
2, in the record recursion.
3, recursive in the right subtree.
4. Print the current root.
So, we can draw the shape of this binary tree:
Then we know that the post-order traversal is: AEFDHZMG
Building a tree based on the pre-order and sequence: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
According to the original topic using vector<int> method obtained, the result memory is super.
treenode* Buildtree (vector<int>& preorder, vector<int>& inorder) {if (preorder.size ()! = Inord
Er.size ()) return NULL;
if (0 = = Preorder.size ()) return NULL;
int Leftlen, Rightlen;
Vector<int>::iterator Rootpos = Find (Inorder.begin (), Inorder.end (), preorder[0]);
Leftlen = Rootpos-inorder.begin ();
Rightlen = Inorder.size ()-1-leftlen;
TreeNode *root = new TreeNode (preorder[0]);
Vector<int> Leftpreorder, Leftinorder, Rightpreorder, Rightinorder;
Leftpreorder.assign (Preorder.begin () + 1, Preorder.begin () + 1 + leftlen);
Leftinorder.assign (Inorder.begin (), Inorder.begin () + Leftlen);
Rightpreorder.assign (Preorder.begin () + 1 + leftlen, preorder.end ());
Rightinorder.assign (Inorder.begin () + 1 + leftlen, inorder.end ());
Root-left = Buildtree (Leftpreorder, Leftinorder); Root-right = Buildtree (Rightpreorder, RIghtinorder);
return root; }
Change the subscript, the code passes.
Class Solution {public:treenode* Buildsubtree (vector<int>& preorder, vector<int>& inorder, int p
Ospre, int posin, int len) {if (0 = = len) return NULL;
int Leftlen, Rightlen;
Vector<int>::iterator Rootpos = Find (Inorder.begin (), Inorder.end (), Preorder[pospre]);
Leftlen = Rootpos-(inorder.begin () + posin);
Rightlen = len-1-Leftlen;
TreeNode *root = new TreeNode (Preorder[pospre]);
Root-left = Buildsubtree (preorder, inorder, Pospre + 1, posin, Leftlen);
Root-right = Buildsubtree (preorder, inorder, Pospre + 1 + leftlen, Posin + 1 + leftlen, Rightlen);
return root; } treenode* Buildtree (vector<int>& preorder, vector<int>& inorder) {if (preorder.size ()! =
Inorder.size ()) return NULL;
if (0 = = Preorder.size ()) return NULL;
Return Buildsubtree (preorder, inorder, 0, 0, preorder.size ()); }
};
Second, the known sequence, post-order traversal, to seek the pre-order.
Replaced the preamble with a post order, the idea is similar to one.
is still the above problem, this time we only give the middle order and post-sequence traversal:
Middle Sequence Traversal: ADEFGHMZ
Post-post traversal: AEFDHZMG
The drawing tree seeking method:
The first step, according to the characteristics of the post-order traversal, we know that the next step to traverse the last node is the root node, that is, the root node is G;
The second step is to observe the sequence traversal adefghmz. The adef on the left side of the root node is necessarily the left subtree of root, and the hmz on the right of G must be the right subtree of root;
The third step is to observe the left subtree adef, and the root node of Zuozi must be the root of the tree leftchild. In the pre-sequence traversal, the root leftchild of the tree is located after root, so the root node of the Zuozi is D;
The fourth step, the same truth, root in the right subtree node HMZ root node can also be obtained through the pre-sequence traversal. In the pre-order traversal, it must be that all the left subtree nodes of root and root are traversed before the right subtree is traversed, and the first node of the left subtree that is traversed is the root node of Zuozi. Similarly, the first node of the right subtree is the root node of the right subtree;
The fifth step, observed that the above process is recursive. First find the root node of the current tree, then divide it into left dial hand tree, right subtree, and then go to the left subtree to repeat the above procedure, and then go to the right subtree to repeat the above procedure. Finally, you can restore a tree.
The process of solving the pre-sequence traversal by direct recursion and the sequence traversal can be concisely expressed as follows:
1 Determine the root, determine the left subtree, and determine the right subtree.
2 recursion in record.
3 recursively in the right subtree.
4 Prints the current root.
In this way, we can draw the shape of a two-fork tree, as shown in the figure above, and we will not repeat it here.
So, the pre-sequence traversal: Gdafemhz
Build the tree according to the sequence and order: https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
Class Solution {public:treenode* Buildsubtree (vector<int>& inorder, vector<int>& postorder, int
Posin, int pospost, int len) {if (0 = = len) return NULL;
int Leftlen, Rightlen;
Vector<int>::iterator Rootpos = Find (Inorder.begin (), Inorder.end (), Postorder[pospost + len-1]);
Leftlen = Rootpos-(inorder.begin () + posin);
Rightlen = len-1-Leftlen;
TreeNode *root = new TreeNode (Postorder[pospost + len-1]);
Root-left = Buildsubtree (Inorder, Postorder, Posin, Pospost, Leftlen);
Root-right = Buildsubtree (Inorder, Postorder, Posin + 1 + leftlen, Pospost + Leftlen, Rightlen);
return root; } treenode* Buildtree (vector<int>& inorder, vector<int>& postorder) {if (Postorder.size ()
! = Inorder.size ()) return NULL;
if (0 = = Postorder.size ()) return NULL; Return Buildsubtree (inorder, postorder, 0, 0, PostordEr.size ()); }
};
Third, the known pre-order, after-order traversal, seeking the middle sequence.
This is not the only. Think about why.