包含min函数的栈 ,二叉树的镜像
包含min函数的栈
问题
定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
代码
# -*- coding:utf-8 -*-
class Solution:
l = []
def push(self, node):
# write code here
self.l.append(node)
def pop(self):
# write code here
d = self.l[-1]
self.l = self.l[:-1]
return d
def top(self):
# write code here
return self.l[-1]
def min(self):
# write code here
return min(self.l)
二叉树的镜像
问题
操作给定的二叉树,将其变换为源二叉树的镜像。
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。代码
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# 返回镜像树的根节点
def Mirror(self, root):
# write code here
if not root:
return None
root.left, root.right = root.right, root.left
self.Mirror(root.left)
self.Mirror(root.right)
return root

更多精彩