106. Construct Binary Tree from Inorder and Postorder Traversal (Tree; DFS)

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

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

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) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        root = NULL;
        if(inorder.empty()) return root;
        root = new TreeNode(0);
        buildSubTree(inorder,postorder,0,inorder.size()-1,0,postorder.size()-1,root);
        return root;

    }
    void buildSubTree(vector<int> &inorder, vector<int>&postorder, int inStartPos, int inEndPos, int postStartPos, int postEndPos,TreeNode * currentNode)
    {
        currentNode->val = postorder[postEndPos]; //后序遍历的最后一个节点是根节点

        //find root position in inorder vector
        int inRootPos;
        for(int i = inStartPos; i <= inEndPos; i++)
        {
            if(inorder[i] == postorder[postEndPos])
            {
                inRootPos = i;
                break;
            }
        }

        //right tree: 是中序遍历根节点之后的部分,对应后序遍历根节点前相同长度的部分
        int newPostPos = postEndPos - max(inEndPos - inRootPos, 0);
        if(inRootPos<inEndPos)
        {
            currentNode->right = new TreeNode(0);
            buildSubTree(inorder,postorder,inRootPos+1,inEndPos,newPostPos,postEndPos-1,currentNode->right);
        }

        //leftTree: 是中序遍历根节点之前的部分,对应后序遍历从头开始相同长度的部分
        if(inRootPos>inStartPos)
        {
            currentNode->left = new TreeNode(0);
            buildSubTree(inorder,postorder,inStartPos,inRootPos-1,postStartPos,newPostPos-1,currentNode->left);
        }
    }
private:
    TreeNode* root;
};
时间: 2024-12-28 08:18:02

106. Construct Binary Tree from Inorder and Postorder Traversal (Tree; DFS)的相关文章

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

106. Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 60461 Total Submissions: 203546 Difficulty: Medium Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not

Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: 115870 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 解题思路

[LeetCode] 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. class Solution { public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { int

44: Construct Binary Tree from Inorder and Postorder Traversal

/************************************************************************/            /*       44:  Construct Binary Tree from Inorder and Postorder Traversal                            */            /*************************************************

leetcode 刷题之路 64 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. 给出二叉树的中序遍历和后序遍历结果,恢复出二叉树. 后序遍历序列的最后一个元素值是二叉树的根节点的值,查找该元素在中序遍历序列中的位置mid,根据中序遍历和后序遍历性质,有: 位置mid以前的序列部分为二叉树根节点左子树中

Construct Binary Tree from Inorder and Postorder Traversal (算法课上的题)

Construct Binary Tree from Inorder and Postorder Traversal 这道题之前算法课上好像遇到过,思路也很简单的. 思路:后序序列的最后一个元素就是树根,然后在中序序列中找到这个元素(由于题目保证没有相同的元素,因此可以唯一找到),中序序列中这个元素的左边就是左子树的中序,右边就是右子树的中序,然后根据刚才中序序列中左右子树的元素个数可以在后序序列中找到左右子树的后序序列,然后递归的求解即可.(在去除了根节点之后,中序遍历和后序遍历的前N个树都是

Leetcode dfs Construct Binary Tree from Inorder and Postorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 14363 Total Submissions: 54254My Submissions Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in t

[leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意:根据二叉树的中序遍历和后序遍历恢复二叉树. 解题思路:看到树首先想到要用递归来解题.以这道题为例:如果一颗二叉树为{1,2,3,4,5,6,7},则中序遍历为{4,2,5,1,6,3,7},后序遍历为{4,5,2,6,7,3,1},我们可以反推回去.由于后序遍历的最后一个节点就是树的根.也就是roo

leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp;amp; Construct Binary Tree f

1.  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. 代码: class Solution { public: TreeNode *buildTr