没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
试读
1页
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。 class Solution: def reConstructBinaryTree(self, pre, tin): if not pre or not tin: return None root = TreeNode(pre.pop(0)) index = tin.index(r
资源详情
资源评论
资源推荐
python练习题练习题
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数
字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
class Solution:
def reConstructBinaryTree(self, pre, tin):
if not pre or not tin:
return None
root = TreeNode(pre.pop(0))
index = tin.index(root.val)
root.left = self.reConstructBinaryTree(pre, tin[:index])
root.right = self.reConstructBinaryTree(pre, tin[index + 1:])
return root
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
class Solution:
def __init__(self):
self.stack1 = [] self.stack2 = [] def push(self, node):
self.stack1.append(node)
def pop(self):
if len(self.stack2) != 0:
return self.stack2.pop()
while len(self.stack1) !=0:
self.stack2.append(self.stack1.pop())
return self.stack2.pop()
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。
输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。
例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
class Solution:
def minNumberInRotateArray(self, rotateArray):
# write code here
length = len(rotateArray)
if length == 0:
return 0
elif length == 1:
return rotateArray[0] else:
for i in range(length-1):
if rotateArray[i] > rotateArray[i+1]:
return rotateArray[i+1] return rotateArray[length-1]
class Solution:
def minNumberInRotateArray(self, rotateArray):
# write code here
if not rotateArray:
return 0
else:
rotateArray.sort()
return rotateArray[0]
Null_Pan
原创文章 23获赞 21访问量 1073
关注
私信
展开阅读全文
作者:Null_Pan
weixin_38688371
- 粉丝: 8
- 资源: 889
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- VR开发的概要介绍与分析
- 自动驾驶定位系列教程七:点云畸变补偿.pdf
- HM2302D-VB一款N-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- HM2302B-VB一款N-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- springboot学生考勤管理系统
- HM2302A-VB一款N-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- HM2301-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- 数据可视化的概要介绍与分析
- HM2301E-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- HM2301D-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论10