[leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal

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


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

相关文章推荐

Construct Binary Tree from Preorder and Inorder Traversal LeetCode 105

题目描述: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may as...
  • bu_min
  • bu_min
  • 2015年05月27日 10:22
  • 257

leetcode 105-106:Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume th...

LeetCode: Construct Binary Tree from Preorder and Inorder Traversal [105]

【题目】 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assu...

剑指offer 面试题6:重建二叉树(Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal) 解题报告

剑指offer 重建二叉树 参与人数:5246  时间限制:1秒  空间限制:32768K 题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍...

【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/pro...

<LeetCode OJ> 105. Construct Binary Tree from Preorder and Inorder Traversal

Total Accepted: 65143 Total Submissions: 224882 Difficulty: Medium Given preorder and inorder t...

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

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

leetcode No105. Construct Binary Tree from Preorder and Inorder Traversal

Question: Given preorder and inorder traversal of a tree, construct the binary tree. 根据树的前序遍历和中序遍历...

LeetCode题解-105-Construct Binary Tree from Preorder and Inorder Traversal

原题 Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume ...

[leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal@Java解题报告

https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal
举报原因:
原因补充:

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