Skip to content

Latest commit

 

History

History
19 lines (18 loc) · 449 Bytes

File metadata and controls

19 lines (18 loc) · 449 Bytes
  • 左节点的下一节点是右节点,右节点的下一节点是其父节点的下一节点的左节点,以此规律递归即可
class Solution {
 public:
  Node* connect(Node* root) {
    if (!root) {
      return nullptr;
    }
    if (root->left) {
      root->left->next = root->right;
      if (root->next) root->right->next = root->next->left;
    }
    connect(root->left);
    connect(root->right);
    return root;
  }
};