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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- A program to check if a binary tree is BST or not
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- K'th Largest Element in BST when modification to BST is not allowed
- Check if the given binary tree has a sub-tree with equal no of 1's and 0's | Set 2
- Check whether a binary tree is a full binary tree or not
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Check whether a given binary tree is skewed binary tree or not?
- Check if a Binary Tree contains duplicate subtrees of size 2 or more
- Check if a Binary Tree is BST : Simple and Efficient Approach
- Check if the Binary Tree contains a balanced BST of size K
- Convert a normal BST to Balanced BST
- Check whether every node of binary tree has a value K on itself or its any immediate neighbours
- Count the nodes of a tree whose weighted string does not contain any duplicate characters
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Check if a Binary Tree is an Even-Odd Tree or not
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Duplicate subtree in Binary Tree | SET 2
- Check if a binary tree is subtree of another binary tree | Set 1
- Check if a binary tree is subtree of another binary tree | Set 2
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
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.