[Cpp]
Struct TreeNode {
Int val;
TreeNode * left;
TreeNode * right;
TreeNode (int x): val (x), left (NULL), right (NULL ){}
};
Class Solution {
// Note:
// You may assume that duplicates do not exist in the tree.
// So we can build an unordered_map O (1) to look up the index in inorder, to divide the left subtree and right subtree
// In postorder.
// Inorder {(inStart) left (inRootIdx-1) root (inRootIdx + 1) right}, postorder {left (inRootIdx-1) right (PostEnd-1) root}
Public:
Unordered_map <int, int> m_Value2Index; // inorder map
Void BuildMap (vector <int> & inorder)
{
M_Value2Index.clear ();
For (int I = 0; I <inorder. size (); ++ I)
M_Value2Index [inorder [I] = I;
}
// Just make sure (inHigh-inLow = preHigh-preLow), there will be no problem then
TreeNode * BuildTreeInPlusPre (vector <int> & inorder, int inLow, int inHigh, vector <int> & preorder, int preLow, int preHigh)
{
If (inLow> inHigh | preLow> preHigh)
Return NULL;
Int rootValue = preorder [preLow];
TreeNode * parent = new TreeNode (rootValue );
Int inRootIdx = m_Value2Index [rootValue];
Parent-> left = BuildTreeInPlusPre (inorder, inLow, inRootIdx-1, preorder, preLow + 1, preLow + inRootIdx-inLow );
Parent-> right = BuildTreeInPlusPre (inorder, inRootIdx + 1, inHigh, preorder, preHigh-(inHigh-inRootIdx-1), preHigh );
Return parent;
}
TreeNode * buildTree (vector <int> & preorder, vector <int> & inorder ){
// Start typing your C/C ++ solution below
// Do not write int main () function
BuildMap (inorder );
Return BuildTreeInPlusPre (inorder, 0, inorder. size ()-1, preorder, 0, preorder. size ()-1 );
}
};
Struct TreeNode {
Int val;
TreeNode * left;
TreeNode * right;
TreeNode (int x): val (x), left (NULL), right (NULL ){}
};
Class Solution {
// Note:
// You may assume that duplicates do not exist in the tree.
// So we can build an unordered_map O (1) to look up the index in inorder, to divide the left subtree and right subtree
// In postorder.
// Inorder {(inStart) left (inRootIdx-1) root (inRootIdx + 1) right}, postorder {left (inRootIdx-1) right (PostEnd-1) root}
Public:
Unordered_map <int, int> m_Value2Index; // inorder map
Void BuildMap (vector <int> & inorder)
{
M_Value2Index.clear ();
For (int I = 0; I <inorder. size (); ++ I)
M_Value2Index [inorder [I] = I;
}
// Just make sure (inHigh-inLow = preHigh-preLow), there will be no problem then
TreeNode * BuildTreeInPlusPre (vector <int> & inorder, int inLow, int inHigh, vector <int> & preorder, int preLow, int preHigh)
{
If (inLow> inHigh | preLow> preHigh)
Return NULL;
Int rootValue = preorder [preLow];
TreeNode * parent = new TreeNode (rootValue );
Int inRootIdx = m_Value2Index [rootValue];
Parent-> left = BuildTreeInPlusPre (inorder, inLow, inRootIdx-1, preorder, preLow + 1, preLow + inRootIdx-inLow );
Parent-> right = BuildTreeInPlusPre (inorder, inRootIdx + 1, inHigh, preorder, preHigh-(inHigh-inRootIdx-1), preHigh );
Return parent;
}
TreeNode * buildTree (vector <int> & preorder, vector <int> & inorder ){
// Start typing your C/C ++ solution below
// Do not write int main () function
BuildMap (inorder );
Return BuildTreeInPlusPre (inorder, 0, inorder. size ()-1, preorder, 0, preorder. size ()-1 );
}
};