Tripadvisor interview question

How do you find duplicates in a binary search tree?

Interview Answers

Anonymous

13 Oct 2013

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

4

Anonymous

11 Oct 2014

can binary search tree have duplicates?

2

Anonymous

18 Feb 2014

Do a inorder traversal and store elements in HashMap with frequency update on encountering duplicates public void duplicateNodes(Node head) { if(head==null) return; if(map.containsKey(head.key)) { map.put(head.key, Integer.parseInt(""+ map.get(head.key) + 1)); } else map.put(head.key, 1); duplicateNodes(head.left); duplicateNodes(head.right); } Map map1 = binarySearchTree.map; Iterator it = map1.entrySet().iterator(); while(it.hasNext()) { Map.Entry pairs = (Map.Entry)it.next(); if(Integer.parseInt(""+pairs.getValue()) > 1) System.out.println("Dup Node " + pairs.getKey()); }

1