Skip to content

Instantly share code, notes, and snippets.

@iamprayush
Created September 17, 2020 14:07
Show Gist options
  • Save iamprayush/31f36b081eced5af87f4f610305bff57 to your computer and use it in GitHub Desktop.
Save iamprayush/31f36b081eced5af87f4f610305bff57 to your computer and use it in GitHub Desktop.
Flatten Binary Tree to Linked List
class Solution:
def flatten(self, root: TreeNode) -> None:
# If current node is null, or it's a leaf, we don't do shit.
if root is None or (root.left is None and root.right is None):
return
if root.left:
self.flatten(root.left)
original_right = root.right
root.right = root.left
root.left = None
rightmost_node = root.right
while rightmost_node.right:
rightmost_node = rightmost_node.right
rightmost_node.right = original_right
if root.right:
self.flatten(root.right)
@iamprayush
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment