Check if a Binary Tree (not BST) has duplicate values
Input : Root of below tree 1 / \ 2 3 \ 2 Output : Yes Explanation : The duplicate value is 2. Input : Root of below tree 1 / \ 20 3 \ 4 Output : No Explanation : There are no duplicates.
A simple solution is to store inorder traversal of given binary tree in an array. Then check if array has duplicates or not. We can avoid the use of array and solve the problem in O(n) time. The idea is to use hashing. We traverse the given tree, for every node, we check if it already exists in hash table. If exists, we return true (found duplicate). If it does not exist, we insert into hash table.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Check if a Binary Tree contains duplicate subtrees of size 2 or more
- Duplicate subtree in Binary Tree | SET 2
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Longest Path with Same Values in a Binary Tree
- Create loops of even and odd values in a binary tree
- Distance between two nodes of binary tree with node values from 1 to N
- Sum of all the child nodes with even parent values in a Binary Tree
- Check whether a given binary tree is skewed binary tree or not?
- Check if a binary tree is subtree of another binary tree | Set 1
- Check whether a binary tree is a full binary tree or not
- Check if a binary tree is subtree of another binary tree | Set 2
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Check if the given binary tree has a sub-tree with equal no of 1's and 0's | Set 2
- Check if the given n-ary tree is a binary tree
- Check whether a given binary tree is perfect or not
- Check if a given Binary Tree is SumTree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.