Pergunta de entrevista da empresa Illusive Networks

given a tree (root node), and an int "maxToDraw", draw the subtree that contains no more then maxToDraw nodes. don't draw partial levels of the tree, meaning that if a node is at level x, at the number of nodes with level <= x is more than maxToDraw, don't draw it.

Resposta da entrevista

Sigiloso

2 de abr. de 2021

if (root == null) "nothing to do" return; queue = new empty queue #seen=1 (root) #drawn= 0 queue.add(root) while (queue is not empty) { if (#seen > maxToDraw) break; node = queue.poll() node .draw() for (child of node) { queue.add(child) #seen ++ } } the idea is we queue the next level of children, incrementing the total #seen. once #seen is larger than maxToDraw we stop