Domanda di colloquio di Meta

Sort a binary tree into an array.

Risposte di colloquio

Anonimo

29 ott 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?

Anonimo

7 nov 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.

Anonimo

26 feb 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.