Skip to content

Instantly share code, notes, and snippets.

@sdpatil
Created August 12, 2017 19:51
Show Gist options
  • Save sdpatil/239fa0fed83c7552b2f72449e8a51bd5 to your computer and use it in GitHub Desktop.
Save sdpatil/239fa0fed83c7552b2f72449e8a51bd5 to your computer and use it in GitHub Desktop.
Compute the right sibling tree
/**
* 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