Assume we take a number `x` and perform any one of the following operations:
```Pearl
a) Divide x by 3 (if it is divisible by 3), or
b) Multiply x by 2
```
After each operation, we write down the result. If we start with `9`, we can get a sequence such as:
```
[9,3,6,12,4,8] -- 9/3=3 -> 3*2=6 -> 6*2=12 -> 12/3=4 -> 4*2=8
```
You will be given a shuffled sequence of integers and your task is to reorder them so that they conform to the above sequence. There will always be an answer. 
```
For the above example:
solve([12,3,9,4,6,8]) = [9,3,6,12,4,8].
```
More examples in the test cases. Good luck!
def solve(arr):
    stack = []
    for num in arr:
        branches = arr.copy()
        branches.remove(num)
        stack = [([num], branches)]
        while len(stack) != 0:
            (current, remaining) = stack.pop()
            if len(remaining) == 0:
                return current
            x = current[len(current) - 1]
            valid_leaves = [leaf for leaf in remaining if x / 3 == leaf or x * 2 == leaf]
            for valid in valid_leaves:
                valid_current = current.copy()
                valid_current.append(valid)
                valid_remaining = remaining.copy()
                valid_remaining.remove(valid)
                stack.append((valid_current, valid_remaining))
                

def solve(arr):
    stack = []
    for num in arr:
        branches = arr.copy()
        branches.remove(num)
        stack = [([num], branches)]
        while len(stack) != 0:
            (current, remaining) = stack.pop()
            if len(remaining) == 0:
                return current
            x = current[len(current) - 1]
            valid_leaves = [leaf for leaf in remaining if x / 3 == leaf or x * 2 == leaf]
            for valid in valid_leaves:
                valid_current = current.copy()
                valid_current.append(valid)
                valid_remaining = remaining.copy()
                valid_remaining.remove(valid)
                stack.append((valid_current, valid_remaining))
                
