Connect Nodes at Same Level
>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| class Solution { vector<Node*> lvl; public: void connect(Node *root, int dep = 0) { if(!root) return; if(lvl.size() == dep) lvl.push_back(root); else { lvl[dep]->nextRight = root; lvl[dep] = root; } connect(root->left, dep + 1); connect(root->right, dep + 1); } };
|