class Solution {
public:
void connect(TreeLinkNode *root) {
if(root==NULL) return ;
TreeLinkNode *start = root;
TreeLinkNode *cur = start;
while(start){
cur = start;
while(cur && cur->left){
cur->left->next = cur->right;
if(cur->next) cur->right->next = cur->next->left;
cur = cur->next;
}
start = start->left;
}
return ;
}
};
public:
void connect(TreeLinkNode *root) {
if(root==NULL) return ;
TreeLinkNode *start = root;
TreeLinkNode *cur = start;
while(start){
cur = start;
while(cur && cur->left){
cur->left->next = cur->right;
if(cur->next) cur->right->next = cur->next->left;
cur = cur->next;
}
start = start->left;
}
return ;
}
};
本文介绍了一种在树形结构中连接相邻节点的方法。通过遍历树的层级,将每一层的左节点与其右节点以及下一个节点的左节点进行链接,从而实现节点之间的横向连接。
773

被折叠的 条评论
为什么被折叠?



