Created
September 17, 2020 14:07
-
-
Save iamprayush/31f36b081eced5af87f4f610305bff57 to your computer and use it in GitHub Desktop.
Flatten Binary Tree to Linked List
This file contains 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 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.youtube.com/watch?v=598gdjE6Quo