/*
// 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;
}
};
*/
class Solution {
public Node connect(Node root) {
if(root==null) return root;
Queue<Node>list=new LinkedList<>();
list.offer(root);
while(!list.isEmpty()){
int len=list.size();
for(int i=0;i<len;i++){
Node tree=list.poll();
if(i==len-1){
tree.next=null;
}
else{
Node new1=list.peek();
tree.next=new1;
}
if(tree.left!=null){list.offer(tree.left);
}
if(tree.right!=null){
list.offer(tree.right);
}
}
}
return root;
}
}
利用维护队列

977

被折叠的 条评论
为什么被折叠?



