Question d’entretien chez Tripadvisor

How do you find duplicates in a binary search tree?

Réponses aux questions d'entretien

Utilisateur anonyme

13 oct. 2013

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

4

Utilisateur anonyme

11 oct. 2014

can binary search tree have duplicates?

2

Utilisateur anonyme

18 févr. 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