问题:
Given inorder and postorder traversal of a tree, construct the binary tree.
解答:
这是非常常规的递归问题。需要注意的是递归函数参数的设计。如果设置为题目早前设定的TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder),那么在大数据情况下 ,会出现调用栈占用内存过多的现象。解决方法也很简单,将子串的左右界限的下标作为参数传递进递归函数即可。
代码:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
TreeNode *buildTreeAux(vector<int> &inorder, int l1, int r1,
vector<int> &postorder, int l2, int r2)
{
int len = r1 - l1 + 1;
if(len <= 0)
return NULL;
if(len == 1)
{
TreeNode* root = new TreeNode(inorder[l1]);
return root;
}
int rootval = postorder[r2];
TreeNode* root = new TreeNode(rootval);
int mid = l1;
for(; mid < r1 + 1; mid++)
{
if(inorder[mid] == rootval)
break;
}
int leftlen = mid - l1;
int rightlen = r1 - mid;
if(leftlen > 0)
root->left = buildTreeAux(inorder, l1, mid - 1, postorder, l2, l2 + leftlen - 1);
if(rightlen > 0)
root->right = buildTreeAux(inorder, mid + 1, r1, postorder, l2 + leftlen, r2 - 1);
return root;
}
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
int len = postorder.size();
TreeNode * root = buildTreeAux(inorder, 0, len - 1, postorder, 0, len - 1);
return root;
}
};