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. 自动化测试--响应请求测试(.net)

    Web运行原理简单地说是“浏览器发送一个HTTP Request到Web服务器上,Web服务器处理完后将结果(HTTP Response)返回给浏览器”. 通常测试一个web api是否正确,可以通过 ...

  2. ASP.NET CORE网站部署到 windows server 的IIS 上去

    章基于我自己经验的一个总结,在windows服务器上部署asp.net core网站.环境是 windows server 2012数据中心版本 第一步先安装 IIS 服务器 接下来就是一路下一步,然 ...

  3. 第3课 进化后的const分析

    C语言中的const const修饰的变量是只读的,本质还是变量 const修饰的局部变量在栈上分配空间(改变这个空间的值,这个变量就会改变) const修饰的全局变量在只读存储区分配空间 const ...

  4. MAC远程桌面连接问题

    如图,始终提示“证书或相关链无效.” 点击“取消” 点击“编辑连接...” 点击“打开” 点击“安全性” 选择“即使验证失败,也始终连接” OK

  5. Redis学习笔记-数据操作篇(Centos7)

    一.基本操作 1.插入数据 127.0.0.1:6379> set name cos1eqlg0 OK 这样就在redis中设置了一个key-value键值对 2.查询数据 127.0.0.1: ...

  6. mac终端下修改MySQL的编码格式--找不到my-default.cnf及my.cnf

    首先请确认正确安装好MySQL. 1- 先配置环境变量path 1.1 打开终端,输入: cd ~ 会进入~文件夹, 1.2 然后输入:touch .bash_profile 回车执行后, 1.3 再 ...

  7. 用Keras 和 DDPG play TORCS(1)

    用Keras 和 DDPG play TORCS(环境配置篇) 原作者Using Keras and Deep Deterministic Policy Gradient to play TORCS ...

  8. PHP安全性漫谈

    最近刚做完两个PHP的网站项目,客户虽然没有安全性的相关需求,但是自己不能放过对自己的要求,何况对以后做电子商务的项目相当有帮助,呵呵,早准备早超生,经过查看PHP 帮助和相关资料~~~~       ...

  9. emacs之切换h/cpp配置

    emacsConfig/switch-file-setting.el (defun switch-c () (global-set-key (kbd "<C-return>&qu ...

  10. Tomcat 容器的设计和实现

    Tomcat 容器是对 Servlet 规范的实现,也称为 Servlet 引擎.在分析 Tomcat 容器的设计和实现之前,首先简单了解一下 Servlet 规范,弄清楚 Tomcat 究竟要实现什 ...