-
-
Save InterviewBytes/68a1ab4630cc09c1467fd61c3fa20a07 to your computer and use it in GitHub Desktop.
Next Right Pointer O(1)
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
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; | |
} | |
} | |
} |
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
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