Solution


  • 0
    N

    ...
    public class Solution {
    public void connect(TreeLinkNode root) {
    if(root == null)
    return;
    Queue<TreeLinkNode> q = new LinkedList<TreeLinkNode>();
    q.add(root);
    int level =0;
    TreeLinkNode temp = null,prev =null;
    while(!q.isEmpty()){
    level = q.size();
    for(int i=0;i<level;i++){
    temp = q.remove();
    if(prev !=null){
    prev.next = temp;
    }
    prev = temp;
    if(temp.left !=null)
    q.add(temp.left);
    if(temp.right !=null)
    q.add(temp.right);
    }
    temp.next = null;
    prev =null;
    level = 0;
    }
    }
    }
    ...


Log in to reply
 

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