Created
May 31, 2016 21:30
-
-
Save mkmojo/92e58c06444d5bab8790a7f0d34713ad to your computer and use it in GitHub Desktop.
Convert binary tree to doubly linked list
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
class Solution: | |
def __init__(self): | |
self.dummy = DoublyListNode(0) | |
self.pre = self.dummy | |
""" | |
@param root, the root of tree | |
@return: a doubly list node | |
""" | |
def bstToDoublyList(self, root): | |
def helper(root): | |
if not root: | |
return root | |
helper(root.left) | |
node = DoublyListNode(root.val) | |
self.pre.next = node | |
node.prev = self.pre | |
self.pre = self.pre.next | |
helper(root.right) | |
helper(root) | |
return self.dummy.next |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To reuse pinter and save space, we can use the following code: