Given preorder and inorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

Solution: Just do it recursively.

     TreeNode *build(vector<int> &preorder, int pstart, int pend, vector<int> &inorder, int istart, int iend) {
TreeNode * root = new TreeNode(preorder[pstart]);
int idx_root = -;
for(int i = istart; i <= iend; i ++) {
if(inorder[i] == preorder[pstart]){
idx_root = i;
break;
}
} if(idx_root == -)
return NULL; int left_size = idx_root - istart;
if(left_size > )
root->left = build(preorder, pstart + , pstart + left_size, inorder, istart, idx_root - ); int right_size = iend - idx_root;
if(right_size > )
root->right = build(preorder, pend - right_size + , pend, inorder, idx_root + , iend); return root;
} TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
if(preorder.size() == || inorder.size() == || preorder.size() != inorder.size())
return NULL; return build(preorder, , preorder.size() -, inorder, , inorder.size() - );
}

Construct Binary Tree from Preorder and Inorder Traversal [LeetCode]的更多相关文章

  1. Construct Binary Tree from Preorder and Inorder Traversal——LeetCode

    Given preorder and inorder traversal of a tree, construct the binary tree. 题目大意:给定一个二叉树的前序和中序序列,构建出这 ...

  2. Construct Binary Tree from Preorder and Inorder Traversal leetcode java

    题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume ...

  3. Construct Binary Tree from Preorder and Inorder Traversal

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

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

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

  6. 【题解二连发】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 ...

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

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

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

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

随机推荐

  1. Java 使用jaxp删除节点

    <?xml version="1.0" encoding="UTF-8" standalone="no"?> <perso ...

  2. Array基本操作

    // defined array object val arr0= ) val arr1= Array(") println(arr1()) arr1()="Hello Spark ...

  3. 为Go Web App 创建一个主页面

    原文地址    大多数web app都有一个相同的布局.这个布局可能包含一个header或者footer,甚至可能包含一个导航菜单.Go的标准库提供一个简单的方式来创建这些基本元素,通过被不同的页面重 ...

  4. 桥接和nat模式区别

    bridged networking(桥接模式) 在这种模式下,VMWare虚拟出来的操作系统就像是局域网中的一台独立的主机,它可以访问网内任何一台机器.在桥接模式下,你需要手工为虚拟系统配置IP地址 ...

  5. 2.在程序中如何实现Cookie信息的设置,读取和删除

    设置:你可以在IE的“工具/Internet选项”的“常规”选项卡中,选择“设置/查看文件”,查看所有保存到你电脑里的Cookies.这些文件通常是以user@domain格式命名的,user是你的本 ...

  6. mongo基本语句

    批量更新 db.test.updateMany({name:'test'},{$set:{value:1}}) 单更新 db.test.update({name:'test'},{$set:{valu ...

  7. How to run a (Tomcat)Java application server on a Azure virtual machine

    http://www.windowsazure.com/en-us/documentation/articles/virtual-machines-java-run-tomcat-applicatio ...

  8. python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例

    python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例 新浪爱彩双色球开奖数据URL:http://zst.aicai.com/ssq/openInfo/ 最终输出结果格 ...

  9. 在虚拟机发布网站,设置服务器外网访问ip端口号

    这是虚机上的发布网站的网站端口号 这一步要在实机设置 做完这一步,在外网就可以访问你刚刚发布的站点了

  10. laravel框架总结(七) -- 数据库操作

      1.使用DB门面进行基本操作 一旦你设置好了数据库连接,就可以使用 DB facade 来进行查找.DB facade 提供每个类型的查找方法:select.update.insert.delet ...