Leetcode 105

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
return DFS(preorder,,inorder,,inorder.size()-);
} TreeNode* DFS(vector<int> preorder,int pos,vector<int> inorder,int ileft,int iright){
if(ileft > iright) return NULL;
int i=;
for(i=ileft;i <= iright;i++){
if(preorder[pos] == inorder[i])break;
}
TreeNode* cur = new TreeNode(preorder[pos]);
cur->left = DFS(preorder,pos+,inorder,ileft,i-);
cur->right = DFS(preorder,pos+i-ileft+,inorder,i+,iright);
return cur;
}
};
Leetcode 105的更多相关文章
- 48. leetcode 105题 由树的前序序列和中序序列构建树结构
leetcode 105题,由树的前序序列和中序序列构建树结构.详细解答参考<剑指offer>page56. 先序遍历结果的第一个节点为根节点,在中序遍历结果中找到根节点的位置.然后就可以 ...
- [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 106 从前序与中序遍历序列构造二叉树 从中序与后序遍历序列构造二叉树
题目: 105 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = ...
- Java实现 LeetCode 105 从前序与中序遍历序列构造二叉树
105. 从前序与中序遍历序列构造二叉树 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中 ...
- 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
原题地址 基本二叉树操作. O[ ][ ] [ ]O[ ] 代码: TreeNode *restore(vector< ...
- leetcode[105] Construct Binary Tree from Inorder and Postorder Traversal
代码实现:给定一个中序遍历和后序遍历怎么构造出这颗树!(假定树中没有重复的数字) 因为没有规定是左小右大的树,所以我们随意画一颗数,来进行判断应该是满足题意的. 3 / \ 2 4 /\ / \1 6 ...
- 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 ...
随机推荐
- [转载] iframe嵌入网页的用法
iframe并不是很常用的,在标准的网页中非常少用.但是有朋友经常问到,下面我简单地介绍一下它的用法,你只要熟练掌握这些参数足矣. <iframe>也应该是框架的一种形式,它与<fr ...
- python基础八---文件操作
1.文件操作 XXXXX.txt 1.文件路径:d:\XXXXX.txt(绝对路径) 2.编码方式:utf-8 gbk 3.操作方式:只读.只写.追加.读写.写读.... 排错: 以什么编码方式储存的 ...
- 关于sqlite使用场景
对于sqlite,实际中从来没有用过,也几乎没有考虑过其使用场景,更不要说专门去研究它了,今天看最新的数据库流行度排行榜的时候,发现sqlite的长期趋势好像一直在第十位左右徘徊,特地搜索了下其使用场 ...
- 05: MySQLdb 原生SQL语句操作数据库
1.1 MySQLdb安装与简介 1.MySQLdb 模块的安装(python3中目前这个模块还不可用)参考博客 1. linux: yum install MySQL-python 2. windo ...
- redis.conf 配置 详解 中文 2.8
# redis version 2.8.19 # 1k => 1000 bytes# 1kb => 1024 bytes# 1m => 1000000 bytes# 1m ...
- 【查看内存参数详解】Linux free -m 详细说明
free 命令相对于top 提供了更简洁的查看系统内存使用情况:$ free total used ...
- 【Git安装】centos安装git
1 yum install git 安装后的默认存放地点/usr/bin/git
- 启动Sql server的服务CMD命令
启动:net start mssqlserver 停止:net stop mssqlserver
- 论文笔记之:End-to-End Localization and Ranking for Relative Attributes
End-to-End Localization and Ranking for Relative Attributes arXiv Paper 摘要:本文提出一种 end-to-end 的属性识别方 ...
- Unity3D学习笔记(一):Unity简介、游戏物体、组件和生命周期函数
Project(工程.项目):工程是把游戏开发当前所需要的资源归类管理用的. Console控制台:日志.报错.调试,右上角,消息过滤 Assets:资源,存储游戏中一切用到的资源 Library:临 ...