链接:https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/
在每个节点时,更新它左右子节点的
n
e
x
t
next
next,充分利用已经有的
n
e
x
t
next
next信息。
C++代码:
class Solution {
public:
Node* connect(Node* root) {
if(root==NULL||root->left == NULL)
return root;
root->left->next = root->right;
if(root->next!=NULL)
root->right->next = root->next->left;
connect(root->left);
connect(root->right);
return root;
}
};