二叉树的相关在线编程(python)

问题一:

  输入一个整数数组,
  判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。
  假设输入的数组的任意两个数字都互不相同。

正确的后序遍历结果:


sequence = [37, 35, 51, 47, 59, 73, 93, 98, 87, 61]

 

python源码:
 1 class Solution:
 2     def VerifySquenceOfBST(self, sequence):
 3         # write code here
 4         if sequence == None or len(sequence) == 0:
 5             return False
 6         length = len(sequence)
 7         root = sequence[length - 1]
 8 
 9         # 在二叉搜索 树中 左子树节点小于根节点   (应该是二叉排序树)
10         for i in range(length):  # 获得左右的分叉口
11             if sequence[i] > root:
12                 break
13 
14         # 二叉搜索树中右子树的节点都大于根节点
15         for j in range(i, length):
16             if sequence[j] < root:
17                 return False
18 
19         # 判断左子树是否为二叉树
20         left = True
21         if i > 0:
22             left = self.VerifySquenceOfBST(sequence[0:i])
23 
24         # 判断 右子树是否为二叉树
25         right = True
26         if i < length - 1:
27             right = self.VerifySquenceOfBST(sequence[i:-1])
28 
29         return left and right
30 
31 
32 if __name__ == '__main__':
33     s = Solution()
34     # sequence = [37, 35, 51, 47, 59, 73, 93, 98, 61, 87]   # 错误的二叉排序树的后续遍历
35     sequence = [37, 35, 51, 47, 59, 73, 93, 98, 87, 61]     # 正确的二叉排序树的后续遍历
36     print(s.VerifySquenceOfBST(sequence))
View Code

 

问题二: 

  

输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)

 1 class TreeNode:
 2     def __init__(self, x):
 3         self.val = x
 4         self.left = None
 5         self.right = None
 6 
 7 
 8 class Solution:
 9     def HasSubtree(self, pRoot1, pRoot2):  # 判断是否有子树
10         if pRoot1 == None or pRoot2 == None:
11             return False
12 
13         result = False
14         if pRoot1.val == pRoot2.val:  # 根节点相等,进行下一步
15             result = self.IsSubtree(pRoot1, pRoot2) # 判断子节点是否“相等”
16 
17         if result == False:
18             res1 = self.HasSubtree(pRoot1.left, pRoot2)
19             res2 = self.HasSubtree(pRoot1.right, pRoot2)
20             result = res1 or res2
21 
22         return result
23 
24     def IsSubtree(self, pRoot1, pRoot2):
25         if pRoot2 == None:
26             return True
27         if pRoot1 == None:
28             return False
29         if pRoot1.val == pRoot2.val:
30             leftt = self.IsSubtree(pRoot1.left, pRoot2.left)
31             rightt = self.IsSubtree(pRoot1.right, pRoot2.right)
32             return leftt and rightt
33         return False
34 
35     def getBSTwithPreTin(self, pre, tin):  # 最终确定树的形状,使用object的形式存储
36         if len(pre) == 0 | len(tin) == 0:  # 任何一个为空树,则返回空
37             return None  # 直接跳出代码
38         root = TreeNode(pre[0])  # 前序遍历直接找到根节点(第一个元素就是根节点)
39         for i, item in enumerate(tin):  # 序号和元素,将中序遍历拆分成左右两支
40             if root.val == item:
41                 root.left = self.getBSTwithPreTin(pre[1:i + 1], tin[:i])
42                 root.right = self.getBSTwithPreTin(pre[i + 1:], tin[i + 1:])
43                 return root
44 
45 
46 if __name__ == '__main__':
47     solution = Solution()
48 
49     preorder_seq = [1, 2, 4, 7, 3, 5, 6, 8]  # 前序遍历
50     middleorder_seq = [4, 7, 2, 1, 5, 3, 8, 6]  # 中序遍历
51     treeA = solution.getBSTwithPreTin(preorder_seq, middleorder_seq)
52 
53     preorder_seq = [1, 2, 3]
54     middleorder_seq = [2, 1, 3]
55     treeB = solution.getBSTwithPreTin(preorder_seq, middleorder_seq)
56     
57     print(solution.HasSubtree(treeA, treeB))
View Code

 

 

 

posted on 2019-03-21 14:22  周健康  阅读(484)  评论(0编辑  收藏  举报

导航