1、题目描述:二叉树的重建、和为某一值的路径、深度、树的子结构、树的镜像等。
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 20 15:20:57 2018
@author: Administrator
"""
class Node():
def __init__(self,elem=-1,lchild=None,rchild=None):
self._elem=elem
self._lchild=lchild
self._rchild=rchild
class BinaryTree():
def __init__(self):
self._root=Node()
self._myQueue=[]
def add(self,elem):#为树添加节点
node=Node(elem)
if self._root._elem==-1:#如果树是空的,则对根节点赋值
self._root=node
self._myQueue.append(self._root)
else:
treeNode=self._myQueue[0]
if treeNode._lchild==None:
treeNode._lchild=node
self._myQueue.append(treeNode._lchild)
else:
treeNode._rchild=node
self._myQueue.append(treeNode._rchild)
self._myQueue.pop(0)#用于叶节点的子节点增加(层次)
def reConstructBinaryTree(self, pre, tin):#1、利用先序和中序重建二叉树--递归
if len(pre)==0:
return
if len(pre)==1:
return Node(pre[0])
else:
flag=Node(pre[0])
flag._lchild=self.reConstructBinaryTree(pre[1:tin.index(pre[0])+1],tin[:tin.index(pre[0])])
flag._rchild=self.reConstructBinaryTree(pre[tin.index(pre[0])+1:],tin[tin.index(pre[0])+1:])
return flag
#返回二维列表,内部每个列表表示找到的路径
def FindPath(self, root, expectNumber):#2、求某一条路径,和为特定值
# write code here
if root==None:
return []
if root and root._lchild==None and root._rchild==None and root._elem==expectNumber:
return [[root._elem]]
result=[]
left=self.FindPath(root._lchild,expectNumber-root._elem)
right=self.FindPath(root._rchild,expectNumber-root._elem)
for i in left+right:#理解不了的话,举一个小例子
result.append([root._elem]+i)
return result
def TreeDepth(self, pRoot):#3、树的深度求解--递归
if pRoot==None:
return 0
if pRoot._lchild==None and pRoot._rchild==None:
return 1
a=self.TreeDepth(pRoot._lchild)
b=self.TreeDepth(pRoot._rchild)
if a>b:
return a+1
else:
return b+1
def HasSubtree(self, pRoot1, pRoot2):#4、树的子结构(两层递归)第一层遍历地去找相同的两个根结点--先序递归
result=False
if pRoot1 and pRoot2:
if pRoot1._elem==pRoot2._elem:
result=self.DoseTree1HaveTree2(pRoot1,pRoot2)
if result==False:
result=self.HasSubtree(pRoot1._lchild,pRoot2)
if result==False:
result=self.HasSubtree(pRoot1._rchild,pRoot2)
return result
def DoseTree1HaveTree2(self,pRoot1,pRoot2):#第二层去找它们的子树是否一样
if pRoot2==None:
return True
if pRoot1==None:
return False
if pRoot1._elem!=pRoot2._elem:
return False
return self.DoseTree1HaveTree2(pRoot1._lchild,pRoot2._lchild) and self.DoseTree1HaveTree2(pRoot1._rchild,pRoot2._rchild)
def Mirror(self, root):#5、树的镜像,交换左右结点--先序递归
if root==None:
return None
if root._lchild==None and root._rchild==None:
return root
root._lchild,root._rchild=root._rchild,root._lchild
if root._lchild:
self.Mirror(root._lchild)
if root._rchild:
self.Mirror(root._rchild)
def level_queue(self,root):#层次遍历
if root==None:
return
myQueue=[]
node=root
myQueue.append(node)
while myQueue:
node=myQueue.pop(0)#这个列表相当于队列
print (node._elem)
if node._lchild:
myQueue.append(node._lchild)
if node._rchild:
myQueue.append(node._rchild)
a=[1,2,4,7,3,5,6,8]
b=[4,7,2,1,5,3,8,6]
tree=BinaryTree() #<class '__main__.BinaryTree'>
t1=tree.reConstructBinaryTree(a,b)#1、此函数返回的是一棵树,<class '__main__.Node'>
#tree.level_queue(tree._root)#输出-1
#tree.level_queue(t1)#输出1,2,3,4,5,6,7,8
#print(tree.FindPath(t1,9))#2
#print (tree.TreeDepth(t1))#3
tree2=BinaryTree()#4、构建另一棵树
for i in [3,5,6,1,2,9,8]:
tree2.add(i)
#print(tree.HasSubtree(t1,tree2._root))
#tree2.Mirror(tree2._root)#5、二叉树的镜像
#tree2.level_queue(tree2._root)
3、总结:主要分析每个问题本质,用递归来实现!