Skip to content

Instantly share code, notes, and snippets.

@aershov24
Created October 13, 2020 05:28
Show Gist options
  • Select an option

  • Save aershov24/7cc7114b73ffe7cb2c2a21c3583f5039 to your computer and use it in GitHub Desktop.

Select an option

Save aershov24/7cc7114b73ffe7cb2c2a21c3583f5039 to your computer and use it in GitHub Desktop.
Markdium-12 Recursion Interview Questions (SOLVED) Devs Have To Nail
def BinaryTreeToDLL(self, node):
#Checks whether node is None
if(node == None):
return;
#Convert left subtree to doubly linked list
self.BinaryTreeToDLL(node.left);
#If list is empty, add node as head of the list
if(self.head == None):
#Both head and tail will point to node
self.head = self.tail = node;
#Otherwise, add node to the end of the list
else:
#node will be added after tail such that tail's right will point to node
self.tail.right = node;
#node's left will point to tail
node.left = self.tail;
#node will become new tail
self.tail = node;
#Convert right subtree to doubly linked list
self.BinaryTreeToDLL(node.right);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment