美文网首页
根据前中后序遍历构造二叉树【题解时空一般】

根据前中后序遍历构造二叉树【题解时空一般】

作者: 锦绣拾年 | 来源:发表于2020-02-11 22:34 被阅读0次

关于二叉树 可以想一想 递归的方法
比如得到根,通过递归得到左右子树的递归指针。

前中序:先构造左子树 再构造右子树

中后序:先构造右子树,再构造左子树
以下LC106和LC105我用了基本一样的方法,两者 时空均击败10+%左右 不是很好吧。

LC105
这个题太奇怪了,如果以下写判空不对

        TreeNode* root;
        if(inorder.empty())
             return root;

直接返回空就好了,难道是因为返回指针的问题?

        if(inorder.size()==0)
            return NULL;

题目:

根据一棵树的前序遍历与中序遍历构造二叉树。

注意:
你可以假设树中没有重复的元素。

例如,给出

前序遍历 preorder = [3,9,20,15,7]
中序遍历 inorder = [9,3,15,20,7]
返回如下的二叉树:

    3
   / \
  9  20
    /  \
   15   7

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题解:

/**
 * 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<int>& preorder, vector<int>& inorder) {
        //左中右 //中左右
        // if(preorder.size()==0&&inorder.size()==0)
        //     return NULL;
        if(inorder.size()==0)
            return NULL;

        TreeNode* root;
        // if(inorder.empty())
        //     return root;

        int tmp=preorder[0];
        root=new TreeNode(preorder[0]);
        //注意这里也可以用递归,然后返回子树树根
        preorder.erase(preorder.begin());//删除第一个元素。
        vector<int> left;
        vector<int> right;
        bool ex=true;
        for(int i=0;i<inorder.size();i++){
            if(ex){
                if(inorder[i]==tmp){
                    ex=false;
                }else{
                    left.push_back(inorder[i]);
                }
            }else{
                right.push_back(inorder[i]);
            }
            
        }
        root->left=buildTree(preorder,left);
        root->right=buildTree(preorder,right);
        return root;
        
        //root->left=buildTree()
        
        
    }
};

LC106

题目

根据一棵树的中序遍历与后序遍历构造二叉树。

注意:
你可以假设树中没有重复的元素。

例如,给出

中序遍历 inorder = [9,3,15,20,7]
后序遍历 postorder = [9,15,7,20,3]
返回如下的二叉树:

    3
   / \
  9  20
    /  \
   15   7

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
/**
 * 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<int>& inorder, vector<int>& postorder) {
        //左中右
        //左右中
        if(inorder.size()==0)
            return NULL;

        TreeNode* root;
        int tmp=postorder.back();
        root=new TreeNode(tmp);
        //注意这里也可以用递归,然后返回子树树根
        postorder.pop_back();//删除最后一个元素。
        vector<int> left;
        vector<int> right;
        bool ex=true;
        for(int i=0;i<inorder.size();i++){
            if(ex){
                if(inorder[i]==tmp){
                    ex=false;
                }else{
                    left.push_back(inorder[i]);
                }
            }else{
                right.push_back(inorder[i]);
            }
            
        }
        root->right=buildTree(right,postorder);
        root->left=buildTree(left,postorder);

        return root;
        
        
    }
};

相关文章

网友评论

      本文标题:根据前中后序遍历构造二叉树【题解时空一般】

      本文链接:https://www.haomeiwen.com/subject/cevffhtx.html