leetcode-mid-Linked list- 105. Construct Binary Tree from Preorder and Inorder Traversal
mycode 43.86%
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def buildTree(self, preorder, inorder):
"""
:type preorder: List[int]
:type inorder: List[int]
:rtype: TreeNode
"""
if len(inorder)==0 or len(preorder)==0:
return
pos = inorder.index(preorder[0])
root = TreeNode(preorder[0])
root.left = self.buildTree(preorder[1:pos+1], inorder[:pos])
root.right = self.buildTree(preorder[pos+1:],inorder[pos+1:])
return root
参考:
可能map的方法会比index快
class Solution(object):
def buildTree(self, preorder, inorder):
"""
:type preorder: List[int]
:type inorder: List[int]
:rtype: TreeNode
"""
if not inorder or not preorder:
return None
map = {val:idx for idx, val in enumerate(inorder)}
pos = map[preorder[0]]
root = TreeNode(preorder[0])
root.left = self.buildTree(preorder[1:pos+1], inorder[:pos])
root.right = self.buildTree(preorder[pos+1:],inorder[pos+1:])
return root
leetcode-mid-Linked list- 105. Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- 【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 ...
- [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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
- (二叉树 递归) 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 从前序与中序遍历序列构造二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 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 OJ 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 由前序和中序遍历建立二叉树 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 ...
随机推荐
- webpack打包错误 ERROR in multi ./src/main.js ./dist/bundle.js
webpack打包错误 ERROR in multi ./src/main.js ./dist/bundle.js:https://www.jianshu.com/p/a55fb5bf75e1
- Comet OJ C1076 [Contest #4]求和
题目 首先我们可以通过经典容斥转化为计算\([1,x]\)的答案. 现在我们考虑一个数的个位对答案的贡献. 每做一次操作都会让个位加上十位然后取模,直到只有个位为止. 我们发现这个过程中,个位数前的系 ...
- 并查集专题: HDU1232畅通工程
畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- kotlin学习(7)高阶函数
高阶函数 以另一个函数作为参数或者返回值的函数被称为高阶函数. 函数类型 //隐式声明(省略了变量类型) val sum = (x:Int, y:Int -> x+y) val action = ...
- 【目标检测+域适应】CVPR18 CVPR19总结
域适应已经是一个很火的方向了,目标检测更不用说,二者结合的工作也开始出现了,这里我总结了CVPR18和CVPR19的相关论文,希望对这个交叉方向的近况有一个了解. 1. 2018_CVPR Domai ...
- Solr知识点摘录
1.Solr和Lucene区别: 2.Solr的下载与安装 3. 4. 5. 6.Solr基础 7. 8.Solr索引操作 9. 10.
- 特殊权限位:suid、sgid、sticky
linux系统特殊权限位 suid.sgid.stickysuid 使任意用户获得用文件属主相同的权限,sgid使用户获得与文件属组相同的权限(通过sgid获得的权限等同于同一用户组的权限) 表示方法 ...
- centOS7 通过nmtui和nmcli图形配置网络服务
一.通过nmtui配置网络参数 Linux系统配置网络参数的方式有很多种,其中最简单最直接的方式就是直接修改网卡配置文件,但这种方式也很容易出错,比如说IPADDR.NETMASK.GATEWAY等参 ...
- Java Annotation 刷课笔记(一)
1.什么是注解? 1.1.Annotation是从JDK5.0引入的新技术 1.2.Annotation的作用: 不是程序本身,可以对程序作出解释(这一点,和注释没什么区别) 可以被其他程序(比如编译 ...
- java 类记载器
转载: https://blog.csdn.net/javazejian/article/details/73413292