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 ...
随机推荐
- Silverlight学习之初始化参数
首先需要在Silverlight的宿主页面添加上initParams,如 <param name="initParams" value="key1=jerry,ke ...
- 小菜的系统框架界面设计-数据的完美呈现(DataGridView扩展)
背景 今天在做系统报表的过程中,我想实现批量操作DataGridView中的数据,在列中加复选框,通过一个事件触发进行全选或取消,可是在外面添加按钮,这种模式虽然能够实现,但是从系统界面设计的角度,美 ...
- 深入浅出MongoDB(一)NoSQL
从本文开始,我们一起学习一下MongoDB相关内容,在学习MongoDB之前,首先要做的就是学习NoSQL. 为什么要学习NoSQL,原因很简单,因为MongoDB是NoSQL数据库的一种,换言之,如 ...
- char引发的血案
char cc = 'j';cc = (char)(cc -32); //注意下,自动转型了System.out.println(cc);
- ContactsContract.CommonDataKinds【Translated By KillerLegend】
http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.html interf ...
- Microsoft SqlServer2008技术内幕:T-Sql语言基础-读书笔记-单表查询SELECT语句元素
1.select语句逻辑处理顺序: FORM WHERE GROUP BY HAVING SELECT OVER DISTINCT TOP ORDER BY 总结: 2.FORM子句的表名称应该带上数 ...
- linq to sql (Group By/Having/Count/Sum/Min/Max/Avg操作符)
Group By/Having操作符 适用场景:分组数据,为我们查找数据缩小范围. 说明:分配并返回对传入参数进行分组操作后的可枚举对象.分组:延迟 1.简单形式: var q = from p in ...
- poj 1459 Power Network
题目连接 http://poj.org/problem?id=1459 Power Network Description A power network consists of nodes (pow ...
- 前端开发规范之html编码规范
原则1.规范 .保证您的代码规范,趋html5,远xhtml,保证结构表现行为相互分离.2.简洁.保证代码的最简化,避免多余的空格.空行,保持代码的语义化,尽量使用具有语义的元素,避免使用样式属性和行 ...
- 一个关于C#中基类与接口混合继承的疑问总结
思路参照 http://www.cnblogs.com/allenlooplee/archive/2004/11/16/64553.html,对原文进行了简化和补充,感谢原作者. 问题很简单,如下所示 ...