/* // Definition for a Node. class Node { public int val; public Node left; public Node right; public Node next; public Node() {} public Node(int _val) { val = _val; } public Node(int _val, Node _left, Node _right, Node _next) { val = _val; left = _left; right = _right; next = _next; } }; */
classSolution { public Node connect(Node root) { if (root == null) { returnnull; } Queue<Node> queue = newLinkedList<>(); queue.offer(root); while (!queue.isEmpty()) { intsize= queue.size(); Nodeprev=null; for (inti=0; i < size; i++) { Nodecur= queue.poll(); if (prev != null) { prev.next = cur; } prev = cur; if (cur.left != null) { queue.offer(cur.left); } if (cur.right != null) { queue.offer(cur.right); } } } return root; } }