Solution 1
相当于在每层遍历的时候按顺序加入链接指针,可以在 0102. Binary Tree Level Order Traversal 上进行改动。
- 时间复杂度: O ( N ) O(N) O(N),其中 N N N为输入序列的节点个数,线性遍历处理输入
- 空间复杂度: O ( 2 ⌊ log N ⌋ ) O(2^{\lfloor \log N \rfloor}) O(2⌊logN⌋),其中 N N N为输入序列的节点个数,因为最坏情况下,最后一层全满,整个队列最大占用就是最后一层
/*
// 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 != nullptr) {
queue<Node*> q;
q.push(root);
// cout << "ROOT " << root->val << endl;
while (!q.empty()) {
int numNodeLevel = q.size();
for(int i = 0; i < numNodeLevel; i++) {
auto now = q.front();
q.pop();
// cout << "NOW " << now->val << endl;
if (i < numNodeLevel - 1) {
// 最后一个不链接
now->next = q.front();
}
auto left = now->left;
if (left != nullptr) {
// cout << "LEFT " << left->val << endl;
q.push(left);
}
auto right = now->right;
if (right != nullptr) {
// cout << "RIGHT " << right->val << endl;
q.push(right);
}
}
}
}
return root;
}
};
Solution 2
【参考官方】
由于本题给定的是一个完美二叉树,因此会有这样的情况出现:
- 当前节点为上一层节点的左节点,其next链接为对应的右节点
- 当前节点为上一层节点的右节点,其next链接为上一层节点的next节点的左节点
因此,可以通过上一层处理下一层的方式实现,其优势在于不需要维护一个额外队列
- 时间复杂度: O ( N ) O(N) O(N),其中 N N N为输入序列的节点个数,线性遍历处理输入
- 空间复杂度: O ( 1 ) O(1) O(1),仅维护常数个线性变量
/*
// 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 != nullptr) {
auto nodeRoot = root; // 同层的最左节点,第一个节点
while (nodeRoot->left != nullptr) {
// 遍历这一层节点组织成的链表,为下一层的节点更新 next 指针
auto now = nodeRoot;
while (now != nullptr) {
// 左节点,next为右节点
now->left->next = now->right;
// 右节点,next为同层下一个左节点
if (now->next != nullptr) {
now->right->next = now->next->left;
}
now = now->next; // 考察下一个节点
}
nodeRoot = nodeRoot->left; // 前往下一层,直接去最左节点的做节点(下一层的最左节点)
}
}
return root;
}
};
Solution 3
Solution 1的Python实现
"""
# Definition for a Node.
class Node:
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
self.val = val
self.left = left
self.right = right
self.next = next
"""
class Solution:
def connect(self, root: 'Node') -> 'Node':
if root is not None:
q = collections.deque([root])
while q:
numNodeLevel = len(q)
for i in range(numNodeLevel):
now = q.popleft()
if i < numNodeLevel - 1:
now.next = q[0]
left, right = now.left, now.right
if left is not None:
q.append(left)
if right is not None:
q.append(right)
return root
Solution 4
Solution 2的Python实现
"""
# Definition for a Node.
class Node:
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
self.val = val
self.left = left
self.right = right
self.next = next
"""
class Solution:
def connect(self, root: 'Node') -> 'Node':
if root is not None:
nodeRoot = root
while nodeRoot.left is not None:
now = nodeRoot
while now is not None:
now.left.next = now.right
if now.next is not None:
now.right.next = now.next.left
now = now.next
nodeRoot = nodeRoot.left
return root