Interview Question
Country: United States
/**
* Definition for binary tree with next pointer.
* public class TreeLinkNode {
* int val;
* TreeLinkNode left, right, next;
* TreeLinkNode(int x) { val = x; }
* }
*/
public class Solution {
public void linkSibling(TreeLinkNode root) {
TreeLinkNode currhead = root;
while (currhead!=null) {
while (currhead!=null && currhead.left==null && currhead.right==null)
currhead = currhead.next;
TreeLinkNode dummy = new TreeLinkNode(0);
TreeLinkNode tail = dummy;
while (currhead!=null) {
if (currhead.left!=null) {
tail.next = currhead.left;
tail = tail.next;
}
if (currhead.right!=null) {
tail.next = currhead.right;
tail = tail.next;
}
currhead = currhead.next;
}
currhead = dummy.next;
}
}
}
Here is edufyme link of my code
- Somil August 05, 2015w w w.edufyme.com/code?id=c81e728d9d4c2f636f067f89cc14862c