您的位置:首页 > 其它

106. Construct Binary Tree from Inorder and Postorder Traversal

2016-05-19 10:54 375 查看
题目:

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

Note:

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

题意:

给定中序遍历跟后序遍历构建完整二叉树

note:

可以假设该二叉树中没有重复元素

思路一:

与106. Construct Binary Tree from Preorder and inorder Traversal题目类似,采用递归的方法实现二叉树的构建。

例子:前序遍历序列:1 2 4 5 3 6 7

中序遍历序列:4 2 5 1 6
3 7

后序遍历序列:4 5 26 7 31

代码:40ms

<span style="background-color: rgb(246, 246, 246);">/**
 * 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) {
        int inorderSize = inorder.size();
        int postorderSize = postorder.size();

        if (inorderSize == 0 || inorderSize != postorderSize)
            return NULL;

        TreeNode *root = recursionBuild(inorder, 0, inorderSize-1, postorder, 0, postorderSize-1);

        return root;
    }

    TreeNode* recursionBuild(vector<int>& inorder, int inLeft, int inRight, vector<int>& postorder, int postLeft, int postRight)
    {
        if (inLeft > inRight)
            return NULL;

        int i = 0;
        TreeNode *node = new TreeNode(postorder[postRight]);

        for (i = inLeft; i <= inRight; i++)
        {
            if (inorder[i] == node->val)
                break;
        }

        node->left  = recursionBuild(inorder, inLeft, i-1, postorder, postLeft, postLeft+i-inLeft-1);
        node->right = recursionBuild(inorder, i+1, inRight, postorder, postLeft+i-inLeft, postRight-1);

        return node;
    }
};
</span>
思路二:

与106.
Construct Binary Tree from Preorder and inorder Traversal题目类似,采用堆栈的方法实现二叉树的构建。

代码:16ms
/**
 * 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(postorder.empty()){
            return nullptr;
        }
        
        int size = postorder.size();
        TreeNode* root = new TreeNode(postorder[size-1]);
        TreeNode* cur = root;
        stack<TreeNode*> stacks;
        stacks.push(root);
        int flag = 1;
        int post_index = size-2;
        int in_index = size-1;
        
        while(post_index>=0){
            if(!stacks.empty() && stacks.top()->val==inorder[in_index]){
                cur = stacks.top();
                flag = 0;
                stacks.pop();
                in_index--;
            }else{
                TreeNode* tmp = new TreeNode(postorder[post_index]);
                if(flag==1){
                    cur->right = tmp;
                    cur = cur->right;
                }else{
                    cur->left = tmp;
                    cur = cur->left;
                    flag = 1;
                }
                stacks.push(tmp);
                post_index--;
            }
        }
        
        return root;
    }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: