Created
October 29, 2021 02:16
-
-
Save tolinwei/65a88e03ea5a8a81ea856d531f132524 to your computer and use it in GitHub Desktop.
Flatten BinaryTree
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
public void flatten(TreeNode root) { | |
while (root != null) { | |
if (root.left != null) { | |
// find the right most in left subtree | |
TreeNode rightMostInLeft = root.left; | |
while (rightMostInLeft.right != null) { | |
rightMostInLeft = rightMostInLeft.right; | |
} | |
rightMostInLeft.right = root.right; | |
root.right = root.left; | |
root.left = null; | |
} | |
root = root.right; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment