# Can anyone help? Java, try to solve in iterative way like the first question

• I know my solution is a long one, but it comes to me at the beginning since this problem can be solved by the same idea as the previous one (which use complete binary tree instead of any binary tree)

so my solution derived from the complete one is

``````    public void connect(TreeLinkNode root) {
if (root == null) {return;}
while (start != null) {
while (curt != null) {
if (curt.left != null && curt.right != null) {curt.left.next = curt.right;}
if (curt.next != null) {
if (curt.right != null) {
if (temp.left != null) {curt.right.next = temp.left;}
else if (temp.right != null) {curt.right.next = temp.right;}
}
else if (curt.left != null) {
if (temp.left != null) {curt.left.next = temp.left;}
else if (temp.right != null) {curt.left.next = temp.right;}
}
}
curt = curt.next;
}
start = start.left;
}
return;
}
``````

which derived from the previous solution

``````    public void connect(TreeLinkNode root) {
if (root == null) {return;}
while (start != null) {
while (curt != null) {
if (curt.left != null) {curt.left.next = curt.right;}
if (curt.right != null && curt.next != null) {curt.right.next = curt.next.left;}
curt = curt.next;
}
if (start.left != null) {start = start.left;}
else {start = start.right;}
}
}
``````

but somehow it doesn't work, it pass 30 test cases but fail at the 30/61
in which the test case is

Output:
{1,#,2,3,#,4,5,6,#,7,#}
Expected:
{1,#,2,3,#,4,5,6,#,7,8,#}

can anyone help? thx a lot

• well when I review this problem, I notice that the temp can have 0 sons, but temp.next's sons may be curt.son's next, so the solution could not find the next node, then it sucks

• @monster-gump Then why would you not update your solution? Your thought is almost but you have to retrieve the next by a `loop` instead of a simple `next`.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.