LeetCode_Construct Binary Tree from Inorder and Postorder Traversal

一.题目

Construct Binary Tree from Inorder and Postorder Traversal

Total Accepted: 33418 Total Submissions: 124726My
Submissions

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

Note:

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

Show Tags

Have you met this question in a real interview?

Yes

No

Discuss

二.解题技巧

这道题和Construct Binary Tree from Preorder and Inorder Traversal类似,都是考察基本概念的,后序遍历是先遍历左子树,然后遍历右子树,最后遍历根节点。

做法都是先根据后序遍历的概念,找到后序遍历最后的一个值,即为根节点的值,然后根据根节点将中序遍历的结果分成左子树和右子树,然后就可以递归的实现了。

上述做法的时间复杂度为O(n^2),空间复杂度为O(1)。

三.实现代码

#include <iostream>
#include <algorithm>
#include <vector>

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/

using std::vector;
using std::find;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution
{
private:
    TreeNode* buildTree(vector<int>::iterator PostBegin, vector<int>::iterator PostEnd,
                        vector<int>::iterator InBegin, vector<int>::iterator InEnd)
    {
        if (InBegin == InEnd)
        {
            return NULL;
        }

        if (PostBegin == PostEnd)
        {
            return NULL;
        }

        int HeadValue = *(--PostEnd);
        TreeNode *HeadNode = new TreeNode(HeadValue);

        vector<int>::iterator LeftEnd = find(InBegin, InEnd, HeadValue);
        if (LeftEnd != InEnd)
        {
            HeadNode->left = buildTree(PostBegin, PostBegin + (LeftEnd - InBegin),
                             InBegin, LeftEnd);
        }

        HeadNode->right = buildTree(PostBegin + (LeftEnd - InBegin), PostEnd,
                                LeftEnd + 1, InEnd);

        return HeadNode;
    }
public:
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder)
    {
        if (inorder.empty())
        {
            return NULL;
        }

        return buildTree(postorder.begin(), postorder.end(), inorder.begin(),
                         inorder.end());

    }
};

四.体会

这道题主要考察的是基本概念,并没有很复杂的算法在里面,可以算是对于二叉树的遍历的进一步理解。

版权所有,欢迎转载,转载请注明出处,谢谢

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-05 01:04:39

LeetCode_Construct Binary Tree from Inorder and Postorder Traversal的相关文章

[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以前的序列部分为二叉树根节点左子树中

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

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

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 @ 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