leetcode105
/**
* 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) {
if (preorder.empty())
return NULL;
int root_index = ;
return createBST(preorder,inorder,root_index,preorder.size()-,root_index);
}
TreeNode* createBST(vector<int>& preorder, vector<int>& inorder, int start, int end, int& index) {
int v = preorder[index];
int i = start;
for (i; i <= end; i++)
if (v == inorder[i])
break;
TreeNode* root = new TreeNode(v);
if (i- >= start)
root->left = createBST(preorder,inorder,start,i-,++index);
if (end >= i+)
root->right = createBST(preorder,inorder,i+,end,++index);
return root;
}
};
这是一道分治的题目,用先序找到根节点,用中序找到其左右子树。
补充一个我认为比较容易理解的版本,使用python 实现:
class Solution:
def buildTree(self, preorder: 'List[int]', inorder: 'List[int]') -> 'TreeNode':
if len(preorder)== or len(inorder)==:
return None val = preorder[]
t = TreeNode(val)
index = inorder.index(val)
t.left = self.buildTree(preorder[:index+],inorder[:index])
t.right = self.buildTree(preorder[index+:],inorder[index+:])
return t
leetcode105的更多相关文章
- leetcode105:Construct Binary Tree from Preorder and Inorder Traversal
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume t ...
- [Swift]LeetCode105. 从前序与中序遍历序列构造二叉树 | 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 ...
- C++版-剑指offer 面试题6:重建二叉树(Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal) 解题报告
剑指offer 重建二叉树 提交网址: http://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6?tpId=13&tq ...
- 【2】【leetcode-105,106】 从前序与中序遍历序列构造二叉树,从中序与后序遍历序列构造二叉树
105. 从前序与中序遍历序列构造二叉树 (没思路,典型记住思路好做) 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [ ...
- 【LeetCode105】Construct Binary Tree from Preorder and Inorder Traversal★★
1.题目 2.思路 3.java代码 //测试 public class BuildTreeUsingInorderAndPreorder { public static void main(Stri ...
- LeetCode105. Construct Binary Tree from Preorder and Inorder Traversal
题目 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = [9,3 ...
- leetcode105 从前序与中序遍历序列构造二叉树
如何遍历一棵树 有两种通用的遍历树的策略: 宽度优先搜索(BFS) 我们按照高度顺序一层一层的访问整棵树,高层次的节点将会比低层次的节点先被访问到. 深度优先搜索(DFS) 在这个策略中,我们采用深度 ...
- Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal前序与中序构造二叉树
根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = [9,3,15 ...
- leetcode105: jump-game-ii
题目描述 给出一个非负整数数组,你最初在数组第一个元素的位置 数组中的元素代表你在这个位置可以跳跃的最大长度 你的目标是用最少的跳跃次数来到达数组的最后一个元素的位置 例如 给出数组 A =[2,3, ...
随机推荐
- 剑指Offer 55. 链表中环的入口结点 (链表)
题目描述 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 题目地址 https://www.nowcoder.com/practice/253d2c59ec3e4bc68d ...
- 莫烦tensorflow(5)-训练二次函数模型并用matplotlib可视化
import tensorflow as tfimport numpy as npimport matplotlib.pyplot as plt def add_layer(inputs,in_siz ...
- js--函数声明和函数表达式--执行顺序
思考: notice:在写JS代码的时候,有两种写法,一种是函数表达式,另外一种是函数声明方式.我们需要重点注意的是,只有函数声明形式才能被提升. function hoistFunction() { ...
- php网站速度性能优化(转)
一个网站的访问打开速度至关重要,特别是首页的打开加载过慢是致命性的,本文介绍关于php网站性能优化方面的实战案例:淘宝首页加载速度优化实践 .想必很多人都已经看到了新版的淘宝首页,它与以往不太一样,这 ...
- Visual Studio AI 离线模型训练(Windows10)
一.序 环境搭建:[查看] samples-for-ai项目下载:[下载],两个版本,一个2018年6月15日前,一个2018年6月15日-16日版本(当前最新版本). 在环境搭建过程中,通过git ...
- Arch Linux pacman 与其他发行版操作比较
原文:https://wiki.archlinux.org/index.php/Pacman/Rosettahttps://old-en.opensuse.org/Software_Managemen ...
- nginx 文档链接
https://www.cnblogs.com/wcwnina/p/8728391.html NGINX简介 http://www.nginx.cn/doc/ ...
- openstry lua redis实现负载均衡
需求: 通过URI地址http://10.0.0.148/test2?uuid=123的uuid参数值的第一位,去实现redis的负载均衡 若uuid第一位为1,那么去10.0.0.148的redis ...
- zabbix_agent添加到系统服务启动(八)
Centos6.5上安装了zabbix_agent后,需要把zabbix_agent添加到系统服务启动,要不然每次要一长串路径再启动,挺麻烦的. 步骤: 1)拷贝zabbix解压包里的zabbix_a ...
- MySql开启远程账户登陆总结
1.更改 "mysql" 数据库里的 "user" 表里的 "host" 项,从"127.0.0.1"改成"% ...