Skip to content

Commit

Permalink
Merge pull request #1010 from shuvojitss/pull-1
Browse files Browse the repository at this point in the history
Create Sum_Tree.py
  • Loading branch information
UTSAVS26 authored Nov 2, 2024
2 parents 2f644a8 + e6356a2 commit de1e55f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Algorithms_and_Data_Structures/Trees/Sum_Tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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")
1 change: 1 addition & 0 deletions Project-Structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
* [Menu Driven Code For Lowest Common Ancestor](Algorithms_and_Data_Structures/Trees/Menu_Driven_Code_for_Lowest_common_ancestor.py)
* [Menu Driven Code For Prefix Tree](Algorithms_and_Data_Structures/Trees/Menu_Driven_Code_for_Prefix_Tree.py)
* [Menu Driven Code For Tree Traversals](Algorithms_and_Data_Structures/Trees/Menu_Driven_Code_for_Tree_Traversals.py)
* [Sum Tree](Algorithms_and_Data_Structures/Trees/Sum_Tree.py)
* Avl Tree Visualizer
* [Main](Algorithms_and_Data_Structures/avl_tree_visualizer/main.py)
* Bellman Ford
Expand Down

0 comments on commit de1e55f

Please sign in to comment.