Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.