第21题:栈的压入、弹出序列
题目说明:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
程序:
设置辅助栈,将压入序列依次压入辅助栈,每次压入后,判断辅助栈栈顶元素与弹出序列首元素是否相等,相等则pop辅助栈栈顶元素以及弹出序列首元素,若最后辅助栈为空,则弹出序列即为该压入序列对应序列。
# -*- coding:utf-8 -*-
class Solution:
def IsPopOrder(self, pushV, popV):
if not pushV and not popV:
return False
new_s = []
for val in pushV:
new_s.append(val)
#上面两行代码等价于:
#for i in range(len(pushV)):
# new_s.append(pushV[i])
while popV and new_s[-1] == popV[0]:
new_s.pop()
popV.pop(0)
if not new_s:#如果辅助栈为空的话,则表示正确
return True
else:
return False
知识点:
n个元素入栈,有多少种出栈序列?
使用卡特兰数可以求解:
第22题:从上往下打印二叉树
题目说明:从上往下打印出二叉树的每个节点,同层节点从左至右打印。
程序:
打印二叉树层序序列。
使用数组保存结点数据,使用队列辅助。
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# 返回从上到下每个节点值列表,例:[1,2,3]
def PrintFromTopToBottom(self, root):
# write code here
if root == None:
return []
l = []
queue = []
queue.append(root)
while queue:
top = queue.pop(0)
l.append(top.val)
if top.left:
queue.append(top.left)
if top.right:
queue.append(top.right)
return l
知识点:
二叉树的遍历分为两种,一是深度优先遍历(DFS),包括前序遍历、中序遍历以及后序遍历;二是广度优先遍历(BFS),即层序遍历。
# ------ coding:utf-8 -------
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
1.前序遍历
def pre_order_recursive(root_node):
if not root_node:
return
print root_node.val
pre_order_recursive(root_node.left)
pre_order_recursive(root_node.right)
2.中序遍历
def mid_order_recursive(root_node):
if not root_node:
return
mid_order_recursive(root_node.left)
print root_node.val
mid_order_recursive(root_node.right)
3.后序遍历
def post_order_recursive(root_node):
if not root_node:
return
post_order_recursive(root_node.left)
post_order_recursive(root_node.right)
print root_node.val
第23题:二叉搜索树的后序遍历序列
题目说明:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
程序:
二叉搜索树,又称二叉查找树,也称二叉排序树。
定义:二叉排序树或者是一棵空树;或者是具有下列性质的二叉树:(1)若它的左子树不空,则左子树上左右结点的值均小于它的根结点的值;(2)若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;(3)它的左、右子树也分别为二叉排序树。
显然,后序序列尾元素即为该数的根结点,那么,比根结点小的元素即为该树左子树结点,比根结点大的元素即为该树右子树结点。如果左右子树存在,那么递归分析左右子树是否满足上述性质,满足返回True,否则返回False。
# -*- coding:utf-8 -*-
class Solution:
def VerifySquenceOfBST(self, sequence):
# write code here
l_s = len(sequence)
if sequence == []:
return False
root = sequence[-1]
left = 0
while sequence[left] < root:
left += 1
right = left
for i in range(right, l_s-1):
if sequence[i] < root:
return False
right += 1
if left == 0:#说明该二叉搜索树只有右子树
return True
else:
left_tree = self.VerifySquenceOfBST(sequence[:left])
if right == left:#说明该二叉搜索树只有左子树
return True
else:
right_tree = self.VerifySquenceOfBST(sequence[left:right])
return left_tree and right_tree#如果存在左右子树,那么左右子树均为二叉搜索树,结果为True,否则为False
知识点:
二叉排序树的中序遍历序列是有序的。
第24题:二叉树中和为某一值的路径
题目说明:输入一颗二叉树的根节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前)
程序:
使用子方法AllRoad()递归将所有路径返回到主方法,判断路径结点之和是否等于输入整数
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# 返回二维列表,内部每个列表表示找到的路径
def FindPath(self, root, expectNumber):
# write code here
if root == None:
return []
result = []
end = []
self.AllRoad(root, result, [root.val])
for i in range(len(result)):
if sum(result[i]) == expectNumber:
end.append(result[i])
return end
def AllRoad(self, root, result, path):#result中包含所有路径
if root.left == None and root.right == None:
result.append(path)
if root.left != None:
self.AllRoad(root.left, result, path + [root.left.val])
if root.right != None:
self.AllRoad(root.right, result, path + [root.right.val])
使用子方法递归返回路径结点之和
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# 返回二维列表,内部每个列表表示找到的路径
def FindPath(self, root, expectNumber):
# write code here
if root == None:
return []
self.s = expectNumber
result = []
self.FindRoad(result, root, [root.val])
return result
def FindRoad(self, result, root, path):
if root.left == None and root.right == None and sum(path) == self.s:
result.append(path)
if root.left:
self.FindRoad(result, root.left, path + [root.left.val])
if root.right:
self.FindRoad(result, root.right, path + [root.right.val])
知识点:
递归打印二叉树所有路径
class TreeNode:
def __init__(self, value):
self.val = value
self.left = None
self.right = None
class Solution:
def binaryTreePaths(self, root):
if root == None:
return []
result = []
self.AllRoad(result, root, [root.val])
return result
def AllRoad(self, result, root, path):
if root.left == None and root.right == None:
result.append(path)
if root.left:
self.AllRoad(result, root.left, parth + [root.left.val])
if root.right:
self.AllRoad(result, root.right, path + [root.right.val])