【第22期】观点:IT 行业加班,到底有没有价值?

[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;
    }
};

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

相关文章推荐

LeetCode106—Construct Binary Tree from Inorder and Postorder Traversal

LeetCode106—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 that duplicates do not exist in the tree. 思路 思路和<a href="http://blog.csdn.net/sunnyyoona/article/

[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]Construct Binary Tree from Inorder and Postorder Traversal

新博文地址: <h2 class="postTitle"

LeetCode 105/106 Construct Binary Tree from Preorder/Postorder and Inorder Traversal

一:LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal 题目: Given preorder an...
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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