Last active
December 31, 2015 16:59
-
-
Save linzhp/8017195 to your computer and use it in GitHub Desktop.
Google on-site 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
Node lastVisted = null; | |
void convertTree(Node root) { | |
if (root == null) { | |
return; | |
} | |
convertTree(root.left); | |
if (lastVisited != null && lastVisited.right == null) { | |
lastVisited.right = root; | |
lastVisited.t |= RIGHT_CONVERTED; | |
} | |
if (root.left == null) { | |
root.left = lastVisited; | |
root.t |= LEFT_CONVERTED; | |
} | |
lastVisited = root; | |
convertTree(root.right); | |
} | |
class Node { | |
Node next, random; | |
char data; | |
} | |
Node duplicateLinkedList(Node head) { | |
Node current = head; | |
Node newHead = null; | |
Node newCurrent; | |
while (current != null) { | |
Node newNode = new Node(current.data); | |
if (newHead == null) { | |
newHead = newNode; | |
} | |
if (newCurrent != null) { | |
newCurrent.next = newNode; | |
} | |
newCurrent = newNode; | |
newCurrent.r = current; | |
Node oldCurrent = current; | |
current = current.next; | |
oldCurrent.next = newCurrent; | |
} | |
newCurrent = newHead; | |
while (newCurrent != null) { | |
newCurrent.r = newCurrent.r.r.next; | |
newCurrent = newCurrent.next; | |
} | |
return newHead; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment