binary tree | 重建二叉树

一、

题目:

105. Construct Binary Tree from Preorder and Inorder Traversal

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

解答:

/**
 * 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() != inorder.size() || preorder.empty() || inorder.empty() )
            return NULL;
        TreeNode * inRoot = myHelper(preorder, inorder, 0, 0, inorder.size() - 1);
        return inRoot;
    }
    //递归的调用myHelper(),先构建根的左子树,再构建根的右子树
    TreeNode* myHelper(vector<int> & preorder, vector<int>& inorder, int preRootPos, int inLeftPos, int inRightPos)//前序的根节点位置preRootPos,此根节点在中序中的左边界位置inLeftPos, 右边界位置inRightPos
    {
        if(inLeftPos > inRightPos)
            return NULL;
        int inRootPos, rootVal;
        rootVal = preorder[preRootPos];
        //寻找根节点rootVal在inorder中的位置inRootPos
        for(int i = inLeftPos; i <= inRightPos; ++i)//int inLeftPos, int inRightPos 都是索引 <=
        {
            if(rootVal == inorder[i])
                inRootPos = i;
        }
        //计算inRootVal到inLeftPOS的长度,即rootVal为根的左子树的长度
        int leftLen = inRootPos - inLeftPos;
        
        TreeNode* inRoot = new TreeNode(rootVal);
        inRoot->left = myHelper(preorder, inorder, preRootPos + 1, inLeftPos, inRootPos - 1);
        inRoot->right = myHelper(preorder, inorder, preRootPos + leftLen + 1, inRootPos + 1, inRightPos);
        return inRoot;
    }
};

二、

题目:

106. Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

For example, given

inorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]

Return the following binary tree:

    3
   / \
  9  20
    /  \
   15   7

解答:

/**
 * 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() != postorder.size() || inorder.empty() || postorder.empty())
            return nullptr;
        TreeNode * inRoot;
        inRoot = myHelper(inorder, postorder, postorder.size() - 1, 0, inorder.size() - 1);
        return inRoot;
    }
    TreeNode* myHelper(vector<int>& inorder, vector<int> & postorder, int postRootPos, int inLeftPos, int inRightPos)
    {
        if(inLeftPos > inRightPos)
            return nullptr;
        int inRootPos, rootVal;
        rootVal = postorder[postRootPos];
        for(int i = inLeftPos; i <= inRightPos; ++i)
            if(rootVal == inorder[i])
                inRootPos = i;
        int rightLen = inRightPos - inRootPos;
        TreeNode* inRoot = new TreeNode(rootVal);
        inRoot->left = myHelper(inorder, postorder, postRootPos - rightLen - 1, inLeftPos, inRootPos - 1);
        inRoot->right = myHelper(inorder, postorder, postRootPos - 1, inRootPos + 1, inRightPos);
        return inRoot;
        
    }
};

猜你喜欢

转载自blog.csdn.net/u012426298/article/details/81125137