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. C# xml压缩包不解压的情况下解析xml内容

    string sourceFilePath = @"E:\文件拷贝\xx\3773\3773.zip"; FileInfo fileInfo = new FileInfo(sour ...

  2. http://www.miniui.com/demo/#src=datagrid/celledit.html

    http://www.miniui.com/demo/#src=datagrid/celledit.html   jQuery MiniUI Demo

  3. session跟cookies区别

    Session和Cookie的使用总结: Session和cookie都是asp.Net中的内置对象,至于他们有什么区别,在这里就不在多说,现在来说说一些比较实用点的东西: 我们知道网站都有一个后台管 ...

  4. 网站禁止右键点击js

    <script>        function stop() {            return false;        }        document.oncontextm ...

  5. [已解决]Eclipse 插件Maven在使用 add dependency,找不到包,解决办法

    以Eclipse版本[Version: Luna Release (4.4.0),]为例, 依次打开:Window >show view > other > Maven Reposi ...

  6. python学习笔记十三 JS,Dom(进阶篇)

    JS介绍 JavaScript 是属于网络的脚本语言!JavaScript 被数百万计的网页用来改进设计.验证表单.检测浏览器.创建cookies,以及更多的应用:JavaScript 是因特网上最流 ...

  7. [问题2014S07] 解答

    [问题2014S07]  解答  (本解答由沈启帆同学提供) 由复旦高代教材 P265 引理 7.4.1 知 \(F(P_i(\lambda)^{e_i})\) 的不变因子组为 \[1,\cdots, ...

  8. MAC地址泛洪攻击测试

    测试环境:kali系统(2个kali分别作攻击人和目标用户) win7系统(主机) 1.步配置FTP设置用户名密码 2.在攻击kali端测试网络的连通性 3.测试tpf是否正常 开始泛洪 4.开始抓包 ...

  9. JS常规的验证代码 - 手机号,邮箱,字符串查找

    //在字符串中执行查找 function isDisgit(s){ var reg = /^[0-9]{1,20}$/; var result = reg.exec(s); //如果格式不正确,返回n ...

  10. linux 修改home目录下的中文目录名为英文

    编辑home/下的 .config/user-dirs.dirs,把所有的中文名称修改为英文名称 在home目录下创建对应的英文名称路径 运行 xdg-user-dirs-update 重启机器