116. Populating Next Right Pointers in Each Node
Medium ↗ Open on LeetCode Source: NeetCode 150
Quick review
- Pattern
- Trees
- Time
- O(N)
- Space
- O(1)
Standard Solution (C++)
SummaryGiven a perfect binary tree where every node has a `next` pointer, link each node's `next` to the node immediately to its right on the same level, with the rightmost node on each level pointing to `nullptr`.
Approach
- Start at the root and process level by level using the already-wired
nextpointers as a linked list for each level. - For each node
curon the current level, wirecur->left->next = cur->right(same parent cross) andcur->right->next = cur->next->left(cross-parent cross, when a right neighbor exists). - Descend to the leftmost child of the current level to begin the next level pass.
- Because the tree is perfect, every internal node always has both children, so no null-child guards are needed beyond checking when we've exhausted a level. This runs in O(n) time with O(1) extra space (no queue).
#include <cstddef>
using namespace std;
// Node definition provided by the judge; reproduced here for compilation.
class Node {
public:
int val;
Node* left;
Node* right;
Node* next;
Node() : val(0), left(nullptr), right(nullptr), next(nullptr) {}
Node(int _val) : val(_val), left(nullptr), right(nullptr), next(nullptr) {}
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 root;
// `levelStart` is the leftmost node of the level we are currently wiring.
Node* levelStart = root;
while (levelStart->left) { // stop when we reach a leaf level
// Walk every node on this level using its next pointers.
for (Node* cur = levelStart; cur; cur = cur->next) {
// Connect left child to right child (same parent).
cur->left->next = cur->right;
// Connect right child to the left child of the next parent,
// if a right neighbour exists on this level.
if (cur->next)
cur->right->next = cur->next->left;
}
// Drop down to the leftmost node of the next level.
levelStart = levelStart->left;
}
return root;
}
};
Time O(n) Space O(1)
A canonical reference solution. Problem statement © LeetCode — see the official link above.
My Solution (C++)
/*
// 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 root;
Node* l=root->left;
Node* r=root->right;
Node* n=root->next;
if(l){
l->next=r;
if(n){
r->next=n->left;
}
connect(l);
connect(r);
}
return root;
}
};
// Time Complexity: O(N)
// Space Complexity: O(1) Time O(N) Space O(1)
My own solution and notes, as I wrote them.
▶ Run it — editable in-browser playground C++
Your solution. A driver couldn't be auto-generated for this signature — add a main() that calls the method and prints the result, then Run.
C++ ▶ Runnable Couldn't auto-generate a driver for this signature — edit to add one.
Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.