/*// 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{publicNodeconnect(Noderoot){if(root==null){returnroot;}Deque<Node>q=newArrayDeque<>();q.offer(root);while(!q.isEmpty()){Nodep=null;for(intn=q.size();n>0;--n){Nodenode=q.poll();if(p!=null){p.next=node;}p=node;if(node.left!=null){q.offer(node.left);}if(node.right!=null){q.offer(node.right);}}}returnroot;}}
/** * Definition for a Node. * type Node struct { * Val int * Left *Node * Right *Node * Next *Node * } */funcconnect(root*Node)*Node{ifroot==nil{returnroot}q:=[]*Node{root}forlen(q)>0{varp*Nodeforn:=len(q);n>0;n--{node:=q[0]q=q[1:]ifp!=nil{p.Next=node}p=nodeifnode.Left!=nil{q=append(q,node.Left)}ifnode.Right!=nil{q=append(q,node.Right)}}}returnroot}
/*// Definition for a Node.public 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; }}*/publicclassSolution{publicNodeConnect(Noderoot){if(root==null){returnnull;}varq=newQueue<Node>();q.Enqueue(root);while(q.Count>0){Nodep=null;for(inti=q.Count;i>0;--i){varnode=q.Dequeue();if(p!=null){p.next=node;}p=node;if(node.left!=null){q.Enqueue(node.left);}if(node.right!=null){q.Enqueue(node.right);}}}returnroot;}}
/*// 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{privateNodeprev,next;publicNodeconnect(Noderoot){Nodenode=root;while(node!=null){prev=null;next=null;while(node!=null){modify(node.left);modify(node.right);node=node.next;}node=next;}returnroot;}privatevoidmodify(Nodecurr){if(curr==null){return;}if(next==null){next=curr;}if(prev!=null){prev.next=curr;}prev=curr;}}
/** * Definition for a Node. * type Node struct { * Val int * Left *Node * Right *Node * Next *Node * } */funcconnect(root*Node)*Node{node:=rootvarprev,next*Nodemodify:=func(curr*Node){ifcurr==nil{return}ifnext==nil{next=curr}ifprev!=nil{prev.Next=curr}prev=curr}fornode!=nil{prev,next=nil,nilfornode!=nil{modify(node.Left)modify(node.Right)node=node.Next}node=next}returnroot}
/*// Definition for a Node.public 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; }}*/publicclassSolution{privateNodeprev,next;publicNodeConnect(Noderoot){Nodenode=root;while(node!=null){prev=null;next=null;while(node!=null){modify(node.left);modify(node.right);node=node.next;}node=next;}returnroot;}privatevoidmodify(Nodecurr){if(curr==null){return;}if(next==null){next=curr;}if(prev!=null){prev.next=curr;}prev=curr;}}