[leetcode]106. Construct Binary Tree from Inorder and Postorder Traversal

原创 2016年08月29日 20:18:37
/**
 * 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 len=inorder.size();
        if(len==0)
            return {};
        else
            return build(inorder,postorder,0,len-1,0,len-1);
    }
    TreeNode* build(vector<int>& inorder, vector<int>& postorder,int inL,int inR, int postL,int postR) 
    {
        if(inR<inL||postR<postL)
            return {};
            
        TreeNode* root=new TreeNode(postorder[postR]);
        
        if(inL==inR||postL==postR)
            return root;
            
        int mid;
        for(int i=inL;i<=inR;i++)
        {
            if(inorder[i]==postorder[postR])
                {
                    mid=i;
                    break;
                }
        }
        
        root->left=build(inorder,postorder,inL,mid-1, postL,postL+mid-inL-1);
        root->right=build(inorder,postorder,mid+1,inR,postL+mid-inL,postR-1);
        
        return root;
    }
};

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

相关文章推荐

【LeetCode-面试算法经典-Java实现】【106-Construct Binary Tree from Inorder and Postorder Traversal(构造二叉树II)】

【106-Construct Binary Tree from Inorder and Postorder Traversal(通过中序和后序遍历构造二叉树II)】【LeetCode-面试算法经典-J...

Construct Binary Tree from Inorder and Postorder Traversal - LeetCode 106

题目描述: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may a...

leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal-递归

原题链接:106. Construct Binary Tree from Inorder and Postorder Traversal 【思路】 本题考查根据中序遍历和后序遍历构造二叉树。其基本思路...

leetcode题解-105 && 106. Construct Binary Tree from Preorder (PostOrder) and Inorder Traversal

其实这两道题目就是从二叉树的前序遍历(后序遍历)和中序遍历数组中恢复出二叉数即可。我们知道二叉树的三种遍历方法后本题就变得很简单了。 先序遍历: 若二叉树为空,则不进行任何操作...

leetcode_middle_94_106. Construct Binary Tree from Inorder and Postorder Traversal

题意: 给出一个二叉树的中序遍历和后序遍历的结果,还原构造出这颗树。 分析: 首先我们应该搞清楚中序遍历和后序遍历的原理。 其次,我们应该举个例子,用人脑的思维方式,来完成这个过程...

【LEETCODE】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 t...

[LeetCode]*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 tha...

LeetCode#106. Construct Binary Tree from Inorder and Postorder Traversal

106. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers...

Leetcode_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 tha...

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal 中序和后序恢复二叉树

1 解题思想这道题和105的思想,基本就是一致的,这里将会说的比较简单,之说下不同 Leetcode 105. Construct Binary Tree from Preorder and Ino...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)