[LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal_Medium tag: Tree Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
For example, given
preorder = [3,9,20,15,7]
inorder = [9,3,15,20,7]
Return the following binary tree:
3
/ \
9 20
/ \
15 7 这个题目的思路就是利用preorder 和inorder的两种特性, 我们可以发现, preorder[0] 总是root, 然后inorder 里面根据之前的root, 可以将inorder分为left tree和right tree,
然后return root, recursive call即可. 1. Constraints
1) pre or in can be empty 2. ideas
recursively DFS, 分别得到root, root.left & root.right, 返回root 3. Code
class Solution:
def constructBT(self, preorder, inorder):
if not preorder or not inorder: return #note 别忘了not before inorder
root, index = TreeNode(preorder[0]), inorder.index(preorder[0])
root.left = self.constructBT(preorder[1:1+index], inorder[:index])
root.right = self.constructBT(preorder[1+index:], inorder[index+1:])
return root
4. Test cases
1) edge case
2)
preorder = [3,9,20,15,7]
inorder = [9,3,15,20,7]
[LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal_Medium tag: Tree Traversal的更多相关文章
- [LeetCode] 106. Construct Binary Tree from Postorder and Inorder Traversal_Medium tag: Tree Traversal
Given inorder and postorder 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 (用先序和中序树遍历来建立二叉树)
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
原题地址 基本二叉树操作. O[ ][ ] [ ]O[ ] 代码: TreeNode *restore(vector< ...
随机推荐
- vs2017默认以管理员运行
1. 打开VS的安装目录,找到devenv.exe,右键,选择“兼容性疑难解答”. 2. 选择“疑难解答程序” 3. 选择“该程序需要附加权限” 4. 确认用户帐户控制后,点击测试程序,不然这个对话框 ...
- 洛谷 P1223排队接水【贪心】
题目描述 有n个人在一个水龙头前排队接水,假如每个人接水的时间为Ti,请编程找出这n个人排队的一种顺序,使得n个人的平均等待时间最小. 输入输出格式 输入格式: 输入文件共两行,第一行为n:第二行分别 ...
- php字符串操作
1.字符串的格式化 按照从表单提交数据之后,php处理的不同:接受,显示,存储.也有三种类型的格式化方法. 1.1字符串的接收之后的整理: trim(),ltrim(),rtrim() 当数据从表单中 ...
- C和C指针小记(八)-操作符、左值右值
1.移位操作符 移位操作符分为左移操作符(<<)和右移操纵符(>>) 对于无符号数:左右位移操作都是逻辑位移 对于有符号数:到底是采用逻辑位移还是算术位移取决于编译器.如果一个 ...
- nginx伪静态之try_files和rewrite讲解
服务器脚本以php为例 一.伪静态是个啥? 1.说起伪静态基本上搞web开发的人,多多少少都有了解与使用,有人会说什么时候会使用伪静态?使用原生的url地址不是蛮好的吗,确实是这样的,其实 ...
- 9 tips to improve spoken english
https://www.youtube.com/watch?v=FUW_FN8uzy0 1. don't translate everything into your native language ...
- 2016年蓝桥杯省赛A组c++第7题(图论)
/* 有12张连在一起的12生肖的邮票,规格是3*4,即: 1111 1111 1111 现在你要从中剪下5张来,要求必须是连着的.(仅仅连接一个角不算相连) */ /* 思路: 先将所有五个一组的情 ...
- [ovs][libvirt][virtio][qemu] vhost user client 排障
ovs-dpdk + qemu 使用vhostuserclient通信,连路不通,libvirt报错如下: -- ::: starting up libvirt version: , package: ...
- Flink - FlinkKafkaProducer010
https://ci.apache.org/projects/flink/flink-docs-release-1.3/dev/connectors/kafka.html 使用的方式, DataStr ...
- Java代码中获取Json的key值
测试json字符串: {"access_token":"hkbQl5o_l67dZ7_vJRATKBwTLk9Yj5QyMuOJThAr8Baj0xWf4wxW1p4ym ...