My easy Solution with 0ms rumtime


  • 0
    B
    public void connect(TreeLinkNode root) {
           Queue<TreeLinkNode> queue = new LinkedList<>();
    		if (root==null) {
    			return ;
    		}
    		queue.add(root);
    		while (!queue.isEmpty()) {
    			//System.out.println("sdfsdf");
    			Queue<TreeLinkNode> tmp = new LinkedList<>();
    			TreeLinkNode lastNode = queue.poll();
    			while(lastNode!=null){
    				//System.out.println(queue.isEmpty());
    				if (lastNode.left!=null) {
    					tmp.offer(lastNode.left);
    				}
    				if (lastNode.right!=null) {
    					tmp.offer(lastNode.right);
    				}
    				lastNode.next = queue.poll();
    				lastNode=lastNode.next;
    			}
    			//System.out.println("out");
    			queue.addAll(tmp);
    		}
        }

Log in to reply
 

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