Follow up to Problem "populating Next right pointersin each Node".
What if the given tree could is any binary tree? Would your previous solution still work?
Note:
For example,
Given The following binary tree,
1 / 2 3 /\ 4 5 7
After calling your function, the tree is should look like:
1, NULL / 2, 3, null /\ 4-> 5, 7, NULL
Basically consistent with populating Next right pointers with each node is only when you add a child node to the queue, you judge the code as follows:
public class Solution {public void connect (Treelinknode root) { if (root==null) return; int Count=1;int level=0; Queue<treelinknode> que =new linkedlist<treelinknode> (); Que.offer (root); while (Que.isempty ()!=true) { level=0;for (int i=0;i<count;i++) {root=que.peek (); Que.poll (); if (i<count-1) { root.next=que.peek ();} else{ Root.next=null;} if (root.left!=null) {que.offer (root.left); level++;} if (root.right!=null) { que.offer (root.right); level++;}} Count=level;}}}
Populating Next right pointers in each Node II