forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0117.cpp
More file actions
22 lines (21 loc) · 704 Bytes
/
Copy path0117.cpp
File metadata and controls
22 lines (21 loc) · 704 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
void connect(TreeLinkNode *root)
{
if (root == nullptr) return;
if (root->left != nullptr) root->left->next = root->right != nullptr ? root->right : findNext(root->next);
if (root->right != nullptr) root->right->next = findNext(root->next);
connect(root->right);
connect(root->left);
}
private:
TreeLinkNode* findNext(TreeLinkNode* curr)
{
if (!curr) return nullptr;
if (curr->left != nullptr) return curr->left;
if (curr->right != nullptr) return curr->right;
return findNext(curr->next);
}
};