1.

How will you convert a binary tree into a doubly-linked list?

Answer»

In a converted doubly linked list, the left and right pointers in nodes of a binary TREE will be used as previous and next pointers, respectively. The nodes in the doubly linked list must be in the same order as the provided Binary Tree's INORDER. The HEAD node of the doubly linked list must be the first node of Inorder TRAVERSAL (leftmost node in the binary tree).

void btToDLL(Node *root, Node **head_reference, Node **previous){ if (!root) return; // RECURSIVE conversion of left subtree btToDLL(root->left, head_reference, previous); if (*head_reference == NULL) *head_reference = root; else { root->left = *previous; *previous->right = root; } *previous = root; // Recursive conversion of right subtree btToDLL(root->right, head_reference, previous);}

Time Complexity: O(n)
Auxiliary Space: O(1)



Discussion

No Comment Found