insane
insane

Reputation: 729

Convert a BST to sorted list

The problem seems straight forward. You must use the same tree and make the right child pointer as the next pointer in the list.

so the algorithm i used is as follows:

def inorder(node, prev, head):
        if(node == NULL):
            return;
        inorder(node.left, prev, head)

        node.right = prev
        if(!prev):
            head = node
        prev = node 

        inorder(node.right, prev, head)

can anyone point out where exactly i am going wrong because it just doesn't seem to work.

Upvotes: 0

Views: 1078

Answers (1)

btilly
btilly

Reputation: 46445

The first bug that I saw is that you're assigning to head and prev inside of inorder and are hoping that somehow this will affect head and prev inside of previous calls to inorder. But it does not.

What you need to do instead is to have inorder return information that you want, then assign them within the parent call.

Upvotes: 1

Related Questions