Skip to content

Instantly share code, notes, and snippets.

@InterviewBytes
Created June 13, 2017 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save InterviewBytes/68a1ab4630cc09c1467fd61c3fa20a07 to your computer and use it in GitHub Desktop.
Save InterviewBytes/68a1ab4630cc09c1467fd61c3fa20a07 to your computer and use it in GitHub Desktop.
Next Right Pointer O(1)
package com.interviewbytes.trees;
public class NextRightPointer2 {
public void connect(TreeLinkNode root) {
TreeLinkNode start = root;
while (start != null) {
TreeLinkNode current = start;
while (current != null) {
if (current.left != null) current.left.next = current.right;
if (current.right != null && current.next != null) current.right.next = current.next.left;
current = current.next;
}
start = start.left;
}
}
}
package com.interviewbytes.trees;
public class TreeLinkNode {
int val;
TreeLinkNode left, right, next;
TreeLinkNode(int x) {
val = x;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment