My AC java code using queue


  • 0
    Y
    public class Solution {
        public void connect(TreeLinkNode root) {
            Deque<TreeLinkNode> row = new LinkedList<TreeLinkNode>();
            if(root == null) return;
            row.offer(root);
            while(!row.isEmpty()) {
                int size = row.size();
                TreeLinkNode pre = new TreeLinkNode(0);
                for(int i = 0;i<size;i++) {
                    pre.next = row.poll();
                    pre = pre.next;
                    if(pre.left!=null) row.offer(pre.left);
                    if(pre.right!=null) row.offer(pre.right);
                }
    
            }
        }
    }

Log in to reply
 

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