Created
October 13, 2020 05:28
-
-
Save aershov24/7cc7114b73ffe7cb2c2a21c3583f5039 to your computer and use it in GitHub Desktop.
Markdium-12 Recursion Interview Questions (SOLVED) Devs Have To Nail
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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