-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path3-5-1.py
58 lines (52 loc) · 1.47 KB
/
3-5-1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 10 16:01:07 2018
@author: Administrator
"""
class BiTNode:
def __init__(self):
self.data=None
self.lchild=None
self.rchild=None
"""
方法功能:判断两棵二叉树是否相等
参数:root1与root2分别为两棵二叉树的根结点
返回值:true:如果两棵树相等则返回true,否则返回false
"""
def isEqual(root1,root2):
if root1==None and root2==None:
return True
if root1==None and root2!=None:
return False
if root1!=None and root2==None:
return False
if root1.data == root2.data:
return isEqual(root1.lchild,root2.lchild) and isEqual(root1.rchild,root2.rchild)
else:
return False
def constructTree():
root=BiTNode()
node1=BiTNode()
node2=BiTNode()
node3=BiTNode()
node4=BiTNode()
root.data=6
node1.data=3
node2.data=-7
node3.data=-1
node4.data=9
root.lchild=node1
root.rchild=node2
node1.lchild=node3
node1.rchild=node4
node2.lchild=node2.rchild=node3.lchild=node3.rchild= \
node4.lchild=node4.rchild=None
return root
if __name__=="__main__":
root1=constructTree()
root2=constructTree()
equal=isEqual(root1,root2)
if equal:
print "这两棵树相等"
else:
print "这两棵树不相等"