Turn over a new leaf You're given a tree, which in computer science tradition, has the root at the top and leaves at the bottom. The leaf nodes are labelled with numbers. Your goal is to take the special leaf marked -1 and move it up to be the new root.

[3, [[16], -1], [4]]  --&gt;  [[[[4], 3], [16]]]



You can imagine rotating the special leaf to the top and letting the rest of tree hang off of it. Keeping the tree in the plane while rotating it to get the correct left-to-right order of all the branches.

The new tree has all the leaves of the original tree except for -1.  

Input: 

A tree whose leaves are distinct positive integers, except for one leaf of -1. The root of the tree will have at least two branches coming off.

The input is given as a nested list like [3, [[16], -1], [[4]]] or its string representation. Delimiters are optional and up to you, but adjacent numbers need to be separated.

Output:

Output or print the flipped tree in the same format as your input. The order of the list entries must be correct. In-place modification is fine.

If your input/output is a data type, it must be one that prints in the required format by default. Built-ins that basically do the task for you are not allowed.

Test cases:

&gt;&gt; [3, [[16], -1], [4]]
[[[[4], 3], [16]]]

&gt;&gt; [2, -1]
[[2]]

&gt;&gt; [44, -1, 12]
[[12, 44]]

&gt;&gt; [[[[-1]]], [[[[4]]]]]
[[[[[[[[[4]]]]]]]]]

&gt;&gt; [[1, 2, 3], [4, -1, 6], [7, 8, 9]]
[[6, [[7, 8, 9], [1, 2, 3]], 4]]

&gt;&gt; [9, [8, [7, [6, -1, 4], 3], 2], 1]
[[4, [3, [2, [1, 9], 8], 7], 6]]
CJam, 24 24 22 bytes
l~{):T]{s$}$(+T1+}gW&lt;p

Try it online.
Thanks Dennis for removing 2 bytes.
Explanation
l~          e# Read the input.
{           e# Do:
    ):T     e# Save the last item to T.
    ]       e# Wrap everything else (as an array) and the last item into an array,
    {s$}$   e#   where the one with -1 (having &quot;-&quot; if stringified) is the first item.
    (+      e# Insert the second array into the first array as the first item,
            e#   or just move the -1 to the end if the first item is already -1.
    T1+     e# Check if the array before this iteration ended with -1.
}g          e# Continue the loop if it did't.
W&lt;p         e# Remove the -1 and print.