-
Notifications
You must be signed in to change notification settings - Fork 73
/
SUMOFLEAFSINABINARYTREE.PY
73 lines (68 loc) · 1.9 KB
/
SUMOFLEAFSINABINARYTREE.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class BinaryTree:
def __init__(self,data):
self.data=data
self.left=None
self.right=None
import queue
def takeinput():
q=queue.Queue()
print("ENTER THE VALUE OF ROOT : ")
rootdata=int(input())
if rootdata==-1:
return None
root=BinaryTree(rootdata)
q.put(root)
while (not(q.empty())):
curr=q.get()
print("ENTER THE VALUE OF LEFTCHILD OF",curr.data,": ")
leftdata=int(input())
if leftdata!=-1:
leftchild=BinaryTree(leftdata)
curr.left=leftchild
q.put(leftchild)
print("ENTER THE VALUE OF RIGHTCHILD OF",curr.data,": ")
rightdata=int(input())
if rightdata!=-1:
rightchild=BinaryTree(rightdata)
curr.right=rightchild
q.put(rightchild)
return root
def leafs(root,leaf=[]):
if root is None:
return []
if root.left==None and root.right==None:
leaf.append(root.data)
leafs(root.left,leaf)
leafs(root.right,leaf)
return leaf
def sumleaf(leafs):
s=sum(leafs)
return s
def printTree(root):
if root==None:
return
inputq=queue.Queue()
outputq=queue.Queue()
inputq.put(root)
while (not(inputq.empty())):
while (not(inputq.empty())):
curr=inputq.get()
print(curr.data,end=" ")
if curr.left!=None:
outputq.put(curr.left)
if curr.right!=None:
outputq.put(curr.right)
print()
inputq,outputq=outputq,inputq
root=takeinput()
print("BINARY TREE IS :")
printTree(root)
leaf=leafs(root)
leafsum=sumleaf(leaf)
print("THE SUM OF LEAFS IN THE BINARY TREE IS :",leafsum)
print("LEAF IN THE BINARY TREE ARE :",end=" ")
if leaf==None:
print("ZERO")
else:
for i in leaf:
print(i,end=" ")