Created
August 12, 2017 19:51
-
-
Save sdpatil/239fa0fed83c7552b2f72449e8a51bd5 to your computer and use it in GitHub Desktop.
Compute the right sibling tree
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
/** | |
* Problem: Compute the right sibling tree | |
*/ | |
public class ComputeRightSiblingTree { | |
public void constructRightSibling(BinaryTreeNode<String> treeNode){ | |
BinaryTreeNode<String> leftStart = treeNode; | |
while(leftStart != null && leftStart.left != null){ | |
populateLowerLevelNextField(leftStart); | |
leftStart = leftStart.left; | |
} | |
} | |
private void populateLowerLevelNextField(BinaryTreeNode<String> startNode){ | |
BinaryTreeNode<String> iter = startNode; | |
while(iter != null){ | |
iter.left.rightSibling = iter.right; | |
if(iter.rightSibling != null){ | |
iter.right.rightSibling = iter.rightSibling.left; | |
} | |
iter = iter.rightSibling; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment