Topic:
Given Inorder and Postorder traversal of a tree, construct the binary tree.
Note:
Assume that duplicates does not exist in the tree.
A two-fork tree is constructed based on the middle sequence traversal and subsequent traversal results of the binary tree.
Thinking Analysis:
This problem is similar to the idea of the leetcode:construct Binary tree from preorder and inorder traversal.
The last node of the binary tree post-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).
After the post-traversal to remove the last root node, the number of X nodes, which is the left sub-tree after the subsequent traversal of the results, the remaining nodes (definitely y) is the right subtree post-sequential traversal results.
So we get the order of the left and right sub-tree traversal and the result of the middle sequence traversal, so back to the original problem.
C + + Reference 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 Inbegin, vector<int>:: Iterator Inend, vector<int>:: Iterator Postbegin, vector<int>:: Iterator Postend) {if(Postbegin = = postend)return nullptr;The root node (the root node is the last node in the post-order traversal) vector<int>:: Iterator Itroot = Find (Inbegin, Inend, * (Postend-1)); 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 post-order traversal result, 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 (Inbegin, Itroot, Postbegin, Postbegin + leftsize); Root->right = Makenode (Itroot +1, Inend, Postbegin + leftsize, Postend-1);returnRoot } Public: TreeNode *buildtree ( vector<int>&inorder, vector<int>&postorder) {if(Postorder.empty ())return nullptr; TreeNode *root = Makenode (Inorder.begin (), Inorder.end (), Postorder.begin (), Postorder.end ());returnRoot }};
Java Reference Code:
/** * Definition for Binary tree * public class TreeNode {* int val, * TreeNode left, * TreeNode right; TreeNode (int x) {val = x;} *} */ Public class solution { PrivateTreeNodeMakenode(int[] inorder,intInbegin,intInend,int[] Postorder,intPostbegin,intPostend) {if(Postbegin = = postend)return NULL;intindex =0; for(inti = Inbegin; i < inend; i++) {if(Inorder[i] = = Postorder[postend-1]) {index = i; } }intLeftsize = Index-inbegin; TreeNode root =NewTreeNode (Postorder[postend-1]); Root.left = Makenode (inorder, Inbegin, Inbegin + leftsize, Postorder, Postbegin, Postbegin + leftsize); Root.right = Makenode (inorder, index +1, Inend, Postorder, Postbegin + leftsize, Postend-1);returnRoot } PublicTreeNodeBuildtree(int[] inorder,int[] postorder) {if(Postorder.length = =0)return NULL; TreeNode root = Makenode (Inorder,0, Inorder.length, Postorder,0, postorder.length);returnRoot }}
Leetcode:construct Binary Tree from inorder and Postorder traversal