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) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    ///help_buildTree_pi()
    template<typename Iteratorr>
    TreeNode *help_buildTree_pi(Iteratorr pre_first,Iteratorr pre_last,
        Iteratorr in_first,Iteratorr in_last){
        if(pre_first == pre_last) return nullptr;
        if(in_first == in_last) return nullptr;

        auto root = new TreeNode(*pre_first);
        auto inRootPos = find(in_first,in_last,*pre_first);
        auto leftSize = distance(in_first,inRootPos);

        root->left = help_buildTree_pi(next(pre_first),next(pre_first,leftSize+1),
                                        in_first,next(in_first,leftSize));
        root->right = help_buildTree_pi(next(pre_first,leftSize+1),pre_last,next(inRootPos),in_last);
        return root;
    }
    TreeNode* buildTree_pi(vector<int> &preorder,vector<int> &inorder){
        return help_buildTree_pi(preorder.begin(),preorder.end(),inorder.begin(),inorder.end());
    }
};
时间: 2024-10-15 09:14:05

105. Construct Binary Tree from Preorder and Inorder Traversal的相关文章

[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.求解树的子树.找出根节点在中序遍历中的位置,根左边的所有元素就是左子树,根右边的所有元

【leetocde】 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. Subscribe to see which companies asked this question 递归就可以了. #include<algorithm> using namespace std; /** * Defi

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. 题目标签:Array, Tree 题目给了我们preOrder 和 inOrder 两个遍历array,让我们建立二叉树.先来举一个例子,让我们看一下preOrder 和 inOrder的特性. 1 / \ 2        

LeetCode OJ 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. Subscribe to see which companies asked this question 解答 /** * Definition for a binary tree node. * struct TreeNod

105. Construct Binary Tree from Preorder and Inorder Traversal (Tree; DFS)

Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. class Solution { public: TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { root

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

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 node. * public class TreeNode { * int val; *

【一天一道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

Java for 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. 解题思路一: preorder[0]为root,以此分别划分出inorderLeft.preorderLeft.inorderRight.preorderRight四个数组,然后root.left=buildTree(pre