Pergunta de entrevista da empresa Meta

Sort a binary tree into an array.

Respostas da entrevista

Sigiloso

29 de out. de 2016

Traverse the whole tree, insert every node value into an array and then sort the array. Is there any better way to do it?

Sigiloso

7 de nov. de 2016

Is it a binary search tree? Does the tree handle duplicates? Can't we do an in-order traversal of the tree and store the values of the nodes we visit in the array? I think using an arraylist would be more helpful, since if we use an array we have to traverse through the binary tree first to get the size to initiate the array.

Sigiloso

26 de fev. de 2017

The answer depends on whether or not the binary tree is a BST. If not, you may as well just copy all of the nodes over to an array and sort in place.