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 = NULL;
if(inorder.empty()) return root;
root = new TreeNode();
buildSubTree(preorder,inorder,,preorder.size()-,,inorder.size()-,root);
return root; }
void buildSubTree(vector<int> &preorder, vector<int> &inorder, int preStartPos, int preEndPos,int inStartPos, int inEndPos, TreeNode * currentNode) //对于树的递归遍历,如果涉及数组,必定参数中要传递start,end,指明当前子树来源于数组的哪一部分
{
currentNode->val = preorder[preStartPos]; //前序遍历的第一个为根节点 //find root position in inorder vector
int inRootPos;
for(int i = inStartPos; i <= inEndPos; i++)
{
if(inorder[i] == preorder[preStartPos])
{
inRootPos = i;
break;
}
} //分别递归处理左子树和右子树
//left tree:是总序遍历中根节点所在位置之前的部分,对应前序遍历根节点之后相同长度的部分
int newPrePos = preStartPos+max(inRootPos-inStartPos, );
if(inRootPos>inStartPos)
{
currentNode->left = new TreeNode();
buildSubTree(preorder,inorder,preStartPos+, newPrePos,inStartPos,inRootPos-,currentNode->left); //递归调用时,要传递新的start,end
}
//right Tree:是中序遍历根节点所在位置之后的一部分,对应前序遍历从末尾开始相同的长度
if(inRootPos < inEndPos)
{
currentNode->right = new TreeNode();
buildSubTree(preorder,inorder,newPrePos+, preEndPos,inRootPos+,inEndPos,currentNode->right); //递归调用时,要传递新的start,end
} }
private:
TreeNode* root;
};

105. Construct Binary Tree from Preorder and Inorder Traversal (Tree; DFS)的更多相关文章

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

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  2. Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  3. 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...

  4. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  5. 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...

  6. LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  7. LeetCode_Construct Binary Tree from Preorder and Inorder Traversal

    一.题目 Construct Binary Tree from Preorder and Inorder Traversal My Submissions Given preorder and ino ...

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

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

随机推荐

  1. 谷歌浏览器怎么调试js 谷歌浏览器调试javascript教程

    谷歌浏览器是一款由谷歌公司开发的浏览器.谷歌浏览器是一款基于其他开源软件所撰写的.下面小编为大家分享一篇谷歌浏览器调试javascript的教程,希望帮助大家 首先我们打开开发者工具,你可以直接在页面 ...

  2. 20155202 2016-2017-2 《Java程序设计》第8周学习总结

    20155202 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 有Java.util.logging包提供了日志功能相关类与接口,使用日志起点是logger ...

  3. 每天一个linux命令(网络):【转载】ifconfig命令

    许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config).通常需 ...

  4. Codeforces 868F. Yet Another Minimization Problem【决策单调性优化DP】【分治】【莫队】

    LINK 题目大意 给你一个序列分成k段 每一段的代价是满足\((a_i=a_j)\)的无序数对\((i,j)\)的个数 求最小的代价 思路 首先有一个暴力dp的思路是\(dp_{i,k}=min(d ...

  5. 【idea】如何安装jetty容器,并使用。

    参考:https://www.jetbrains.com/idea/help/run-debug-configuration-jetty-server.html背景:web开发当中,我觉得服务层的代码 ...

  6. test20181021 快速排序

    题意 对于100%的数据,\(n,m \leq 10^5\) 分析 考场上打挂了. 最大值就是后半部分和减前半部分和. 最小是就是奇偶相减. 方案数类似进出栈序,就是catalan数 线段树维护即可, ...

  7. ubuntu17.10安装LAMP并测试部署php探针系统

    ubuntu17.10修改密码以及安装LAMP并部署php探针系统 步骤1:ubuntu17.10配置IP (这个版本配置IP方式改变较大,apt-get upgrade更新至最新以前配置方式也可以用 ...

  8. yum安装apache及问题解决

    一.检查服务器上是否已经安装了apache apache在linux系统里的名字是httpd,执行以下命令,如果有返回的信息,则会显示已经安装的软件.如果没有则不会显示其它的信息. rpm -qa h ...

  9. const_cast

    函数原型: const_cast < type-id > ( expression ) 去掉const属性:const_cast<int*> (&num),常用,因为不 ...

  10. Javascript 的数据是什么数据类型?

    Javascript 中的数据都是以 64 位浮点 float 存储的. 所有语言对浮点的精度是很难确定的. 如下代码可以实验到问题. <script> var a = 0.4; var ...