Leetcode#105 Construct Binary Tree from Preorder and Inorder Traversal
基本二叉树操作。
O[ ][ ]
[ ]O[ ]
代码:
TreeNode *restore(vector<int> &preorder, vector<int> &inorder, int pp, int ip, int len) {
if (len <= )
return NULL; TreeNode *node = new TreeNode(preorder[pp]); if (len == )
return node; int leftLen = ;
while (inorder[ip + leftLen] != preorder[pp])
leftLen++;
node->left = restore(preorder, inorder, pp + , ip, leftLen);
node->right = restore(preorder, inorder, pp + leftLen + , ip + leftLen + , len - leftLen - ); return node;
} TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
return restore(preorder, inorder, , , preorder.size());
}
Leetcode#105 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 ...
- (二叉树 递归) 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 ...
- 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 ...
- 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 ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 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 ...
- [leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium)
原题 题意: 根据先序和中序得到二叉树(假设无重复数字) 思路: 先手写一次转换过程,得到思路. 即从先序中遍历每个元素,(创建一个全局索引,指向当前遍历到的元素)在中序中找到该元素作为当前的root ...
- leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal,剑指offer 6 重建二叉树
不用迭代器的代码 class Solution { public: TreeNode* reConstructBinaryTree(vector<int> pre,vector<in ...
- 【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 ...
随机推荐
- PHP判断用户所在国家并跳转对应的目录
<?php // 淘宝API查询国家代码 $url = "http://ip.taobao.com/service/getIpInfo.php?ip=".get_client ...
- 亚信联创--java面试题目总结
这几天投简历,只有两家的HR表示感兴趣.易思卓越和亚信联创,不管怎样如果能有机会面试都一定尽力表现,所以找了找网上的面经,这里先把题目总结一下. 职位要求如下: ------------------- ...
- 了解Unix进程(2)
1. 每个进程都有一个名字,crusher 可以得到: # process name puts $PROGRAM_NAME 10.downto(1) do | num | $PROGRAM_NAME ...
- Delphi 7 里没有加载的控件
在原来版本如D5.D6中使用的控件如Quickrep,FastNet等,在D7中仍然是保留的.只是Delphi没有将他们默认的安装到组件面版中来.这些控件包全部保存在Delphi目录的bin下,文件扩 ...
- 对于java反射的理解
java中的反射是一种强大的工具,它能够创建灵活的代码,这些代码可以在运行时装配,无序在组件之间进行链接. 反射允许在编写与执行时,使程序代码能够接入装载到JVM的类的内部信息,而不是源代码中选定的类 ...
- python 序列化之JSON和pickle详解
JSON模块 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类 ...
- 关于Cygwin——包管理、替换默认终端、同MSYS的比较
(搬运自我在SegmentFault的博客) Cygwin 是一个用于 Windows 的类 UNIX shell 环境. 它由两个组件组成:一个 UNIX API 库,它模拟 UNIX 操作系统提供 ...
- JavaWeb之Servlet: ServletConfig 与 ServletContext
ServletConfig对象 什么是ServletConfig对象 ServletConfig对象,叫Servlet配置对象.主要用于加载配置文件的初始化参数. 创建时机 ServletConfig ...
- opengl基础学习专题 (三) 多边形绘制的几种样式
题外话 聪明人之所以不会成功,是由于他们缺乏坚韧的毅力. ——艾萨克·牛顿(1643年1月4日—1727年3月31日)英国 也许可以理解为 想更深一步的时候,坚持,努力和聪明缺一不可. 挺直腰杆在此向 ...
- 算法系列4《Luhn》
Luhn算法由IBM的Hans Peter Luhn发明,又称为"模10"算法,是一种简单的校验和算法,用来验证识别号,一般会被用于身份证号码,信用卡号.IMEI号.社会保险号的验 ...