题目描述1:包含min函数的栈
定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
Python测试:
// An highlighted block
class Stack:
"""
用列表表示栈,符合先进后出的原则
用两个栈解决:
第一个栈stack正常出入栈
第二个support_stack每次push时,将stack中的最小值push进去,而不是直接push某一个值
每次pop操作,两个栈都进行pop,
这样support_stack[-1]表示当前stack的最小值
"""
def __init__(self):
self.stack = []
self.support_stack = []
self.min_num = None
def push(self, num):
self.stack.append(num)
if self.min_num is None:
self.min_num = num
self.support_stack.append(self.min_num)
elif self.min_num < num:
self.support_stack.append(self.min_num)
else:
self.min_num = num
self.support_stack.append(self.min_num)
def pop(self):
if self.stack:
self.support_stack.pop()
return self.stack.pop()
return
def min(self):
return self.support_stack[-1]
if __name__ == '__main__':
stack = Stack()
current_stack = [11, 7, 8, 9, 5, 12, 15]
for num in (current_stack):
stack.push(num)
print(stack.min())
stack.push(7)
# 输入一个比小值大的
print(stack.min())
stack.push(4)
# 输入一个比小值小的
print(stack.min())
stack.pop()
# 弹出最小值
print(stack.min())
题目描述2:栈的压入、弹出序列
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
Python测试:
// An highlighted block
# class Solution:
# def IsPopOrder(self, pushV, popV):
# # write code here
# if not (pushV and popV and len(pushV) == len(popV)):
# return False
# i=0
# top=0
# while pushV:
# key = self.find(pushV,popV[i])
# print("key: ", key)
# if not (type(key) == int) :
# return False
# if key >= top :
# del pushV[key]
# if key>0:
# top = key-1
# else:
# top = 0
#
# i += 1
# else:
# return False
# print("top_i: ", top, i)
# return True
#
# def find(self,stack,node):
# if not stack:
# return False
# for i in range(0,len(stack)):
# if stack[i] == node :
# return i
# return False
class Solution:
def IsPopOrder(self, pushV, popV):
stack = []
while popV:
if stack and stack[-1] == popV[0]:
print(stack)
stack.pop()
# print(stack)
popV.pop(0)
# print(popV)
elif pushV:
stack.append(pushV.pop(0))
print("push: ",pushV)
print(' ',stack)
else:
return False
return True
if __name__ == '__main__':
solution = Solution()
pushV = [1,2,3,4,5]
popV = [4,3,5,1,2]
print(solution.IsPopOrder(pushV, popV))
题目描述3:从上往下打印出二叉树
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
Python测试:
// An highlighted block
class Node(object):
def __init__(self,data):
self.data = data
self.left = None
self.right = None
def solve(root):
if not root:
return None
# 存放结果
result = []
# 存储要打印的节点
temp = []
temp.append(root)
while temp:
# 打印当前结点,若有子节点,则放入temp队列
curNode = temp.pop(0)
result.append(curNode.data)
if curNode.left:
temp.append(curNode.left)
if curNode.right:
temp.append(curNode.right)
return result
if __name__=='__main__':
node = Node(1)
node.left = Node(2)
node.left.left = Node(3)
node.left.right = Node(4)
node.right = Node(5)
node.right.right = Node(6)
result = solve(node)
print(result)
```
https://blog.csdn.net/qq_34178562/article/details/79880213
```javascript
题目描述4:二叉搜索树的后序遍历序列
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
python测试:
// An highlighted block
-*- coding:utf-8 -*-
class Solution:
def VerifySquenceOfBST(self, sequence):
# write code here
if len(sequence) <= 0:
return False
root = sequence[-1]
length = len(sequence)
if min(sequence) > root or max(sequence) < root:
return True # 二叉树只有一个子树的情况下是后序遍历
index = 0
for i in range(length - 1): # 用index分割左右子树
index = i
if sequence[i] > root:
break
# 由于默认sequence[index]>root所以可以从index+1开始
for j in range(index + 1, length - 1):
if sequence[j] < root:
return False
left = True
right = True
if index > 0: # 存在左子树,递归左子树
left = self.VerifySquenceOfBST(sequence[:index])
if index < length - 1: # 存在右子树,递归右子树
right = self.VerifySquenceOfBST(sequence[index:length - 1])
return left & right # 只有当左右子树都为后序遍历时,结果为后序遍历
if __name__=="__main__":
a = Solution()
array = list(map(int, input().split()))
print(a.VerifySquenceOfBST(array))
知识点:
二叉查找树(Binary Search Tree),又称为二叉搜索树、二叉排序树。其或者是一棵空树;或者是具有以下性质的二叉树:
若左子树不空,则左子树上所有结点的值均小于或等于它的根结点的值
若右子树不空,则右子树上所有结点的值均大于或等于它的根结点的值
左、右子树也分别为二叉排序树
https://blog.csdn.net/u010089444/article/details/70854510
题目描述5:二叉树中和为某一值的路径
Python测试:
// An highlighted block
class Node:
def __init__(self,val):
self.val = val
self.left = None
self.right =None
self.next = None
class Solution:
def __init__(self):
self.onePath = []
self.PathArray = []
def FindPath(self, root, expectNumber):
if root is None:
return self.PathArray
self.onePath.append(root.val)
print(self.onePath)
expectNumber -= root.val
if expectNumber==0 and not root.left and not root.right:
self.PathArray.append(self.onePath[:])
print(' ',self.PathArray)
elif expectNumber>0:
print(" :::")
self.FindPath(root.left,expectNumber)
self.FindPath(root.right,expectNumber)
self.onePath.pop()
return self.PathArray
if __name__ == '__main__':
a = Solution()
node = Node(1)
node.left = Node(2)
node.left.left = Node(3)
node.left.right = Node(9)
node.right = Node(5)
node.right.right = Node(6)
root = node
num = 12
print(a.FindPath(root,num))
总结
包含min函数的栈:
https://blog.csdn.net/qq_38441207/article/details/88235878
栈的压入、弹出序列:
https://blog.csdn.net/qq_38441207/article/details/88237116
从上往下打印出二叉树:https://blog.csdn.net/qq_38441207/article/details/88241319
二叉搜索树的后序遍历序列:https://blog.csdn.net/qq_38441207/article/details/88247201
二叉树中和为某一值的路径:
https://blog.csdn.net/qq_38441207/article/details/88351893