TripAdvisor Interview Question: How do you find duplicates in... | Glassdoor

Interview Question

Software Engineering Intern Interview

How do you find duplicates in a binary search tree?


Interview Answer

3 Answers


Do an in-order traversal and keep track of the last item you saw.

Interview Candidate on Oct 13, 2013

Do a inorder traversal and store elements in HashMap with frequency update on encountering duplicates

public void duplicateNodes(Node head)
    if(head==null) return;
        map.put(head.key, Integer.parseInt(""+ map.get(head.key) + 1));
        map.put(head.key, 1);

Map map1 =;
        Iterator it = map1.entrySet().iterator();
            Map.Entry pairs = (Map.Entry);
            if(Integer.parseInt(""+pairs.getValue()) > 1)
                System.out.println("Dup Node " + pairs.getKey());

Nikhil Agarwal on Feb 17, 2014

can binary search tree have duplicates?

Anonymous on Oct 10, 2014

Add Answers or Comments

To comment on this, Sign In or Sign Up.