forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSum_Tree.py
36 lines (30 loc) · 838 Bytes
/
Sum_Tree.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
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def sum_tree(node):
if node is None:
return 0
if node.left is None and node.right is None:
return node.value
left_sum = sum_tree(node.left)
right_sum = sum_tree(node.right)
if node.value == left_sum + right_sum:
return node.value + left_sum + right_sum
else:
return float('inf') # Marks as not a sum tree
def is_sum_tree(root):
return sum_tree(root) != float('inf')
# Example usage
root = Node(60)
root.left = Node(10)
root.right = Node(20)
root.left.left = Node(4)
root.left.right = Node(6)
root.right.left = Node(7)
root.right.right = Node(13)
if is_sum_tree(root):
print("The tree is a Sum Tree")
else:
print("The tree is not a Sum Tree")