Python program to check if a given binary tree is sumtree














































Python program to check if a given binary tree is sumtree



Description:
A SumTree is a Binary Tree where the value of a node is equal to sum of the nodes present in its left subtree and right subtree. An empty tree is SumTree and sum of an empty tree can be considered as 0. A leaf node is also considered as SumTree.
Get the sum of nodes in left subtree and right subtree. Check if the sum calculated is equal to root%u2019s data. Also, recursively check if the left and right subtrees are SumTrees.
Image result for sumtree



Program:

class Node(object):
def __init__(self,data): self.val = data self.left = None self.right = None def find_sum(root): if root is None: return 0 return (find_sum(root.right) + root.val + find_sum(root.left)) def Check_Sumtree(root): if (root is None) or ((root.left is None) and (root.right is None)): return 1 l_count = find_sum(root.left) r_count = find_sum(root.right) if((root.val == l_count+ r_count) and (Check_Sumtree(root.left) and(Check_Sumtree(root.right)))): return 1 return 0 def main(): root = Node(44) root.left = Node(9) root.right = Node(13) root.left.left = Node(4) root.left.right = Node(5) root.right.left = Node(6) root.right.right = Node(7) if (Check_Sumtree(root)): print('The Given Tree is Sum Tree') else: print('The Given Tree is Not Sum tree') if __name__ == "__main__": main()


Output:
The Given Tree is Sum Tree

More Articles of Bhumika Makwana:

Name Views Likes
Python program to find largest value in each level of binary tree 1713 19
Python program to convert a given binary tree to a tree that holds logical and property 561 12
Python Program to Insert into AVL tree 3449 24
Python program to print the nodes at odd levels of a tree 670 20
Python program to check if a binary tree is binary search tree or not 1640 19
Python program to find smallest value in each level of binary tree 906 15
Implement A Queue With Two Stacks 603 19
Introduction to Python DeepLearning Library (Keras) 748 20
python program to remove duplicate elements from the binary search tree 2164 18
Python program to find largest subtree sum in a tree 988 22
Python program to find an element into binary search tree 611 21
Python program to check if two nodes are cousins in a binary tree 976 18
Python height of the binary search tree 2022 14
Python program to find averages of levels in binary tree 663 18
Python program to understand Red-Black trees and insert nodes to it 698 16
Python program to check if a given graph is tree or not 1641 17
Python program to find depth of the deepest odd level leaf node 696 26
Python program to delete leaf nodes with value as x 1058 28
Python program to find maximum level sum in binary tree 1238 19
Python Program to Calculate Size of a Tree using Recursion 2619 13
Python program to check if an array represents Inorder of Binary Search tree or not 799 29
Print cousins of a given node in Binary Tree 1329 25
Python program to find all duplicate subtrees 931 14
Python program to find lowest Common Ancestor in a binary search tree 565 16
Python program to check if removing an edge can divide a binary tree in two halves 773 15
Python Hands on Deep Learning with Keras 753 19
Python program to find largest subtree having identical left and right subtrees 863 19
Python program to find the given level order traversal of a binary tree, check if the tree is a min-heap 668 18
Python program to check if two trees are mirror 654 22
Python program to check if a given binary tree is sumtree 1079 27
Python program to find distance between two nodes of a binary search tree 1474 12
Python program to convert a binary tree into its mirror tree 915 17
Python program to find an element into AVL tree 3657 11
Python program to check if given preorder, inorder and postorder traversals are of same tree 980 18
Python program to check if leaf traversal of two binary trees is same? 707 20
Python program to check whether a binary tree is a full binary tree or not using recursion 1161 22
Python Program for Deletion into Avl 671 25
Python program to construct tree from given inorder and preorder traversals 1699 27
Python program to convert a binary search tree into a Min-Heap 898 13
Python program to convert ternary expression to a binary tree 903 13
* Python program to insert an element into AVL tree 13 12
Python program to find a pair with given sum in binary search tree 1096 27
Python program to diagonal sum of a binary tree 671 12
Python program to convert an arbitrary binary tree to a tree that holds children sum property 644 15
Python program to find an element into red black tree 1490 25

Comments