public TreeLinkNode PopulateNextRight(TreeLinkNode A) { var result = A; while (A != null) { TreeLinkNode p = A; while (p != null) { if (p.left != null) { p.left.nextRight = p.right; } if (p.right != null && p.nextRight != null) { p.right.nextRight = p.nextRight.left; } p = p.nextRight; } A = A.left; } return(result); }
public TreeLinkNode PopulateNextRight1(TreeLinkNode A) { TreeLinkNode result = A; if (A == null) { return(A); } Queue <TreeLinkNode> queue = new Queue <TreeLinkNode>(); queue.Enqueue(A); queue.Enqueue(null); while (queue.Peek() != null) { int size = queue.Count - 1; while (size-- > 0) { TreeLinkNode node = queue.Dequeue(); node.nextRight = queue.Peek(); if (node.left != null) { queue.Enqueue(node.left); } if (node.right != null) { queue.Enqueue(node.right); } } queue.Dequeue(); queue.Enqueue(null); } return(result); }
public TreeLinkNode(int item) { data = item; left = right = nextRight = null; }