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 ...
随机推荐
- 配置NTP服务ntpd/ntp.conf(搭建Hadoop集群可参考)
本文拟定是在一个局域网内(比如一个Hadoop集群)设定一台NTP服务器作为整个网络的标准时间参考,使用网络(集群)内的所有机器保持时间一致!以下是详细的操作步骤: 1. 修改选定的服务器的本地时间 ...
- SequoiaDB(巨杉数据库)(社区版)安装配置使用图解
SequoaiDB是一款新型企业级分布式非关系型数据库,提供了基于PC服务器的大规模集群数据平台.作为全球第一家企业级文档式 NoSQL分布式数据库,为用户提供了一个高扩展性.高可用性.高性能.易维护 ...
- Redis源码研究--字典
计划每天花1小时学习Redis 源码.在博客上做个记录. --------6月18日----------- redis的字典dict主要涉及几个数据结构, dictEntry:具体的k-v链表结点 d ...
- CSS 设置TABLE 表格 边框
/*table列表 合并边框设置*/ .tablelist { border-collapse:collapse; } /*table列表 设置边框宽度及颜色*/ .tablelist td { bo ...
- Delphi XE5 for android 使用 BITMAP STYLE DESIGNER 改变控件背景
一.BITMAP STYLE DESIGNER 工具集成在IDE开发工具的TOOLS菜单. 使用NEW 新建一个安卓样式.NEW—NEW ANDROID STYLE FOR FIREMONKEY. 这 ...
- 简答的理解C语言中的各种类型函数
1.变参函数 变长参数的函数即参数个数可变.参数类型不定 的函数.最常见的例子是printf函数.scanf函数和高级语言的Format函数.在C/C++中,为了通知编译器函数的参数个数和类型可变(即 ...
- [第四版]用getaddrinfo设置tcp基本连接属性
getaddrinfo getaddrinfo的一个重要功能, 很方便的构造struct sockaddr_in对象, 把繁琐的构造过程隐藏起来 getaddrinfo兼有gethostbyname和 ...
- uva 11186 Circum Triangle<叉积>
链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- 掌握这两个技术点,你可以玩转AppCan前端开发
“AppCan的前端开发其实非常简单,只要掌握两方面的技术即可.一方面是会使用栅格布局完成UI的界面排版,另外一方面就是使用AppCan MVVM模型来完成整个页面构造和用户操作逻辑.” 在2016A ...
- iOS学习之Object-C语言内存管理高级
一.属性的内存管理