/*
// Definition for a Node.
class Node {
public:
int val;
Node* left;
Node* right;
Node* next;
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
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){
return NULL;
}
queue<Node*> now,next;
now.push(root);
while(!now.empty()){
Node* pre=NULL;
while(!now.empty()){
auto p=now.front();
if(pre){
pre->next=p;
}
pre=p;
if(p->left){
next.push(p->left);
}
if(p->right){
next.push(p->right);
}
now.pop();
}
pre->next=NULL;
if(!next.empty()){
swap(now,next);
}
}
return root;
}
};
117. 填充每个节点的下一个右侧节点指针 II
最新推荐文章于 2024-08-15 05:02:54 发布