Python program to check if a binary tree has duplicate values














































Python program to check if a binary tree has duplicate values



# Python program to check if a binary tree has duplicate values

# Create hash table by using defaultdict module of collections package and initialize it with 0(int)
from collections import defaultdict
hash_table=defaultdict(int)

class New_Node(object):
#constructor to create new node with given data/value,left and right pointer as NULL
def __init__(self,value):
self.value=value
self.left=None
self.right=None

# preorder traversal of tree

def preorder(root):
if root:
print(root.value,end=' ')
preorder(root.left)
preorder(root.right)

def Check_Duplicate(root,hash_table):

# for empty tree
if root==None:
return False

# if value is already present in hash_table

if hash_table[root.value]:
return True

# insert current node's data in hash_table

hash_table[root.value]=1

# recursively called the function for left and right subtree

return Check_Duplicate(root.left,hash_table) or Check_Duplicate(root.right,hash_table)

# Driver Code
if __name__=="__main__":
root =New_Node(15)
root.left =New_Node(10)
root.right =New_Node(32)
root.left.left =New_Node(4)
root.left.right =New_Node(9)
root.left.left.left =New_Node(46)
root.left.left.right =New_Node(15)
print("preorder traversal of binary tree is:: ")
preorder(root)
print("\n")
if Check_Duplicate(root,hash_table):
print("it has duplicate values")
else:
print("no duplicate values")

''' OUTPUT::
    preorder traversal of binary tree is ::

    15 10 4 46 15 9 32
    it has duplicate values '''

More Articles of Saurabh Sisodia:

Name Views Likes
Python Imaging Library(ExifTags Module) 413 13
Python Imaging Library(ImageTk Module) 579 20
Python Imaging Library(ImageStat Module) 559 29
Python Imaging Library(ImageSequence Module) 428 25
Python Imaging Library(ImagePath Module) 466 28
Python Imaging Library(ImageMath Module) 326 20
Python Imaging Library(ImageFont Module) 616 27
Python Imaging Library(ImageFilter Module) 472 21
Python Imaging Library(ImageFile Module) 323 11
Python Imaging Library(ImageEnhance Module) 364 12
Python program to check if a binary tree is subtree of another binary tree 598 14
Python Imaging Library(ImageDraw Module) 442 22
Python Imaging Library(ImageColor Module) 358 17
Python Imaging Library(Image Module) 404 20
Introduction Of Python Imaging Library (Pillow) 452 25
Python program to reverse a path in binary search tree using queue 423 19
Python program to find the maximum sum leaf to root path in a binary tree 434 27
Python program to find sum of all the numbers that are formed from root to leaf paths 315 11
Python program to find sum of leaf nodes at minimum level 344 16
Python program to print the path common to the two paths from the root to the two given nodes 390 20
Python program to find if there is a triplet in a Balanced binary search tree that adds to zero 466 14
Python program to find longest path with same values in a binary tree 385 14
Python program to find longest consecutive sequence in binary tree 448 27
Python program to find number of turns to reach from one node to other in binary tree 373 25
Python program to find the closest leaf in a binary tree 450 22
Python Program to find the closest leaf node to a given node 392 16
Python program to count pairs from two binary search trees whose sum is equal to a given value x 347 24
Python program to find pairs with given sum such that pair elements lie in different binary search trees 369 22
Python program to check whether a binary tree is a full binary tree or not without using recursion 283 15
Python program to find difference between sums of odd level and even level nodes of a binary tree 331 17
Python program to print duplicate elements from the binary search tree 337 15
Python program to print middle level of perfect binary tree without finding height 381 20
Python program to print path from root to a given node in a binary tree 857 23
Python program to find distance between two given keys of a binary tree 326 15
Python program to find distance from root to given node in a binary tree 320 15
Python program to check if given sorted sub-sequence exists in binary search tree 257 11
Python program to find kth ancestor of a node in binary tree 339 20
Python program to print ancestors of a given node in binary tree 430 22
Python program to find sum of value in each level of binary tree 386 16
Python program to check if a binary tree is sorted level-wise or not 328 14
Python program to get level of a node in binary tree using recursion 362 15
Python program to find successor for a given key in binary search tree without recursion 330 13
Python program to find k-th smallest element in binary search tree 353 24
Python program to convert binary tree to binary search tree 352 16
Python program to check if a binary tree has duplicate values 688 24

Comments