首页 > 其他分享 > lC116-填充每一个节点的右节点

lC116-填充每一个节点的右节点

时间:2023-02-08 19:05:04浏览次数:42  
标签:Node right curr val 填充 public queue lC116 节点


//leetcode submit region begin(Prohibit modification and deletion)
/*
// 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 {

List<List<Node>> res = new ArrayList<List<Node>>();
public Node connect(Node root) {
if(root == null){
return null;
}

Queue<Node> queue = new LinkedList<Node>();
queue.offer(root);

while(!queue.isEmpty()){
int size = queue.size();
Node prev = null;

for (int i = 0; i < size; i++) {
Node curr = queue.poll();
if(prev!=null){
prev.next = curr;
}
prev = curr;
if(curr.left != null){
queue.offer(curr.left);
}
if(curr.right != null){
queue.offer(curr.right);
}
}
}
return root;
}


}
//leetcode submit region end(Prohibit modification and deletion)



标签:Node,right,curr,val,填充,public,queue,lC116,节点
From: https://blog.51cto.com/u_12550160/6044705

相关文章