LeetCode:Construct Binary Tree from Preorder and Inorder Traversal

要求:通过二叉树的前序和中序遍历序列构建一颗二叉树

代码如下:

 1 struct TreeNode {
 2     int            val;
 3     TreeNode    *left;
 4     TreeNode    *right;
 5     TreeNode(int x): val(x),left(NULL), right(NULL) {}
 6 };
 7
 8 typedef vector<int>::iterator Iter;
 9 TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder)
10 {
11     return buildTreeRecur(preorder.begin(), preorder.end(), inorder.begin(), inorder.end());
12 }
13
14 TreeNode *buildTreeRecur(Iter pstart, Iter pend, Iter istart, Iter iend)
15 {
16     if(pstart == pend || istart == iend)
17         return NULL;
18     int ival = *pstart;
19     Iter ipos = find(istart, iend, ival);
20     TreeNode *res = new TreeNode(ival);
21     res->left = buildTreeRecur(pstart + 1, pstart+1+(ipos-istart), istart, ipos);
22     res->right = buildTreeRecur(pstart+1+(ipos-istart), pend, ipos+1, iend);
23     return res;
24 }
时间: 2024-11-01 21:40:43

LeetCode: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

问题: 给定二叉树的前序和中序遍历,重构这课二叉树. 分析: 前序.中序.后序都是针对于根结点而言,所以又叫作先根.中根.后根(当然不是高跟). 前序:根  左 右 中序:左  根 右 对二叉树,我们将其进行投影,就会发现个有趣的事: 发现投影后的顺序,恰好是中序遍历的顺序,这也就是为什么在构造二叉树的时候,一定需要知道中序遍历,因为中序遍历决定了结点间的相对左右位置关系.所以,对一串有序的数组,我们可以来构建二叉有序数,并通过中序遍历,就可以得到这个有序的数组. 既然中序遍历可以通过根结点将序

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

Leetcode dfs Construct Binary Tree from Preorder and Inorder Traversal

Construct Binary Tree from Preorder and Inorder Traversal Total Accepted: 14824 Total Submissions: 55882My Submissions Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the

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 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 No105. Construct Binary Tree from Preorder and Inorder Traversal

Question: Given preorder and inorder traversal of a tree, construct the binary tree. 根据树的前序遍历和中序遍历,构建二叉树 Algorithm: 前序遍历:根-左-右 中序遍历:左-根-右 举个例子 前序遍历:ABDECFG 中序遍历:DBEAFCG 1.前序遍历的第一个元素A,为根结点 2.在中序遍历找到A,则在中序遍历中A左边是左子树,右边是右子树 3.递归(左右子树同样如此) Accepted Code:

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

leetcode105: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. 解题思路分析: 前序遍历首先遍历根节点,然后依次遍历左右子树 中序遍历首先遍历左子树,然后遍历根结点,最后遍历右子树 根据二者的特点,前序遍历的首节点就是根结点,然后在中序序列中找出该结点位置(index),则该结点之