[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...

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

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

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

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 assum...

LeetCode --- 105. Construct Binary Tree from Preorder and Inorder Traversal

题目链接:Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder travers...

【leetcode】【105】Construct Binary Tree from Preorder and Inorder Traversal

一、问题描述 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 that...

Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal

105. Construct Binary Tree from Preorder and Inorder Traversal Total Accepted: 58238 Total Submis...

leetcode-105. Construct Binary Tree from Preorder and Inorder Traversal

leetcode-105. Construct Binary Tree from Preorder and Inorder Traversal题目: 基本的数据结构知识知道,如果想要恢复一颗数,至少需...

【leetcode】105. Construct Binary Tree from Preorder and Inorder Traversal

【leetcode】105. Construct Binary Tree from Preorder and Inorder Traversal
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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