Skip to content
Advertisement

How do I sort a Binary Search Tree from greatest to least?

I need to return an array of nodes sorted from high to low. At the moment I am trying to implement an inorder traversal which gives me the exact opposite of what I’m looking for.

The tree looks like:

JavaScript

My function looks like:

JavaScript

From this, I receive:

JavaScript

When I need to receive:

JavaScript

Is there a way that I can reverse these nodes or is there a different sorting method that I need/should implement?

Advertisement

Answer

You should just swap the two if statements where you make a recursive call, so that you first visit rightReport and then later leftReport.

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement