105 Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树

给定一棵树的前序遍历与中序遍历,依据此构造二叉树。
注意:
你可以假设树中没有重复的元素。
例如,给出
前序遍历 = [3,9,20,15,7]
中序遍历 = [9,3,15,20,7]
返回如下的二叉树:
    3
   / \
  9  20
    /  \
   15   7
详见:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/

/**
 * 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 is=inorder.size();
        if(is==0||inorder.empty())
        {
            return nullptr;
        }
        int val=preorder[0];
        TreeNode* root=new TreeNode(val);
        vector<int> pre_left,pre_right,in_left,in_right;
        int p=0;
        for(;p<is;++p)
        {
            if(inorder[p]==val)
            {
                break;
            }
        }
        for(int i=0;i<is;++i)
        {
            if(i<p)
            {
                pre_left.push_back(preorder[i+1]);
                in_left.push_back(inorder[i]);
            }
            else if(i>p)
            {
                pre_right.push_back(preorder[i]);
                in_right.push_back(inorder[i]);
            }
        }
        root->left=buildTree(pre_left,in_left);
        root->right=buildTree(pre_right,in_right);
        return root;
    }
};

原文地址:https://www.cnblogs.com/xidian2014/p/8719081.html

时间: 2024-10-08 12:10:55

105 Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树的相关文章

leetcode题解: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 duplicates do not exist in the tree. 说明: 1)二叉树可空 2)思路:a.根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中序序列(InSequence)中查找此根(

【LeetCode每天一题】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 duplicates do not exist in the tree. For example, given preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] Return the following binary tree: 3 / 9 20 /

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 duplicates do not exist in the tree. 思路:首先根据前序遍历得到根节点,然后在中序遍历中得到根节点的位置,左边的为左子树,右边的为右子树. 然后再递归求解左子树和右子树的构造即可.代码如下: /** * Definition for a 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 duplicates do not exist in the tree. 思路 主要是根据前序遍历和中序遍历的特点解决这个题目. 1.确定树的根节点.树根是当前树中所有元素在前序遍历中最先出现的元素. 2.求解树的子树.找出根节点在中序遍历中的位置,根左边的所有元素就是左子树,根右边的所有元

[98]验证二叉搜索树&amp;[105]从前序与中序遍历序列构造二叉树

扯闲话时间...很长一段时间没有刷题了,因为工作做得一团糟,惨遭领导怒批,心理压力大得一批导致工作时间特别长又没产出,所以刷题就搁置了... (小声BB)其实感觉领导有点刀子嘴豆腐心,一面说着"公司没义务从零培养新人,我自己也很久不带新人了",一面又给我讲了好多基础知识... 好了,言归正传,今天分享两道题,同类型的,力扣(leetcode中国)给的标签都是深度优先搜索,但是我都没想出来怎么用深度优先,所以都采用了递归. 这里提一句,曾经有位前辈和我说实际工作中递归并不常用,因为递归长

【一天一道LeetCode】#105. Construct Binary Tree from Preorder and Inorder Traversal

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ Given preorder and inorder traversal of a tree, construct the bin

105. 从前序与中序遍历序列构造二叉树(深搜)

注意: 二叉树中没有重复元素,有重复元素就搞不出来了. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 1

【leetcode 105. 从前序与中序遍历序列构造二叉树】解题报告

前往 中序,后序遍历构造二叉树, 中序,前序遍历构造二叉树 TreeNode* build(vector<int>& preorder, int l1, int r1, vector<int>& inorder, int l2, int r2) { if (l1>r1) return nullptr; int x=preorder[l1], i=0; // 确定当前根节点 for(i=l2;inorder[i]!=x&&i<r2;++i)

105. Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. ============== 基本功: 利用前序和中序构建二叉树 , === code /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : va