题目描述
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
For example, given
preorder = [3,9,20,15,7]
inorder = [9,3,15,20,7]
Return the following binary tree:
3
/ 9 20
/ 15 7
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal
同时也是剑指offer的面试题7。
思想
任务本质就是根据先序和中序序列构造二叉树,那么可以用递归的思想。
buildTree(先序序列,中序序列){
找到根结点root;
找到左子树的先序序列和中序序列;
找到右子树的先序序列和中序序列;
root->left=buildTree(左子树的先序序列,左子树的中序序列);
root->right=buildTree(右子树的先序序列,右子树的中序序列);
}
代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* buildTree(vector& preorder, vector& inorder) {
if(preorder.empty()||inorder.empty())
return NULL;
int presize=preorder.size();
int insize=inorder.size();
TreeNode * root = new TreeNode(preorder[0]); //先序遍历的第一个肯定是根结点
root->left=root->right=NULL;
if(presize==1) //递归序列只剩一个的时候,那这个肯定是叶子结点,同时也是根节点,这也是递归结束标志
if(insize==1)
return root;
else
return NULL;
int rootVal=preorder[0];
int inPoint;
for(int i=0;i newLeftPreorder; //生成新的左子树先序序列
vector newRightPreorder; //生成新的右子树先序序列
for(int i=1;i<=leftLength;++i) //第0个是root节点,1到leftLength是左子树
{
newLeftPreorder.push_back(preorder[i]);
}
for(int i=1+leftLength;i newLeftinorder; //生成新的左子树中序序列
vector newRightinorder; //生成新的右子树中序序列
for(int i=0;ileft=buildTree(newLeftPreorder,newLeftinorder); //递归构造左子树
root->right=buildTree(newRightPreorder,newRightinorder); //递归构造右子树
return root;
}
};