Pergunta de entrevista da empresa Tripadvisor

How do you find duplicates in a binary search tree?

Respostas da entrevista

Sigiloso

13 de out. de 2013

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

4

Sigiloso

11 de out. de 2014

can binary search tree have duplicates?

2

Sigiloso

18 de fev. de 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