Friday, February 15, 2013

Flatten Binary Tree to Linked List

---Flatten the binary tree

ListNode* listHead = new ListNode(0);



ListNode* prep = listHead;

findNextNode(root, prep);

ListNode* currListHead = listHead->next;
 
while(currListHead != NULL){

if(currListHead->next != NULL)

cout << currListHead->val << "->";

else cout << currListHead->val;



currListHead = currListHead->next;


void findNextNode (Node<int>* curr, ListNode* & prep){

if(curr == NULL) {

return;



}

ListNode* currNode = new ListNode(curr->Value);

if (prep != NULL) prep->next = currNode;


prep = currNode;

findNextNode(curr->Left, prep);

findNextNode(curr->Right, prep);

}


No comments:

Post a Comment