您的位置:首页 > Web前端 > Node.js

LeetCode之Populating Next Right Pointers in Each Node II

2015-08-11 19:48 507 查看
/*由于二叉树的层序遍历空间是O(n),可以利用建立的链表进行遍历。
参考自:https://github.com/soulmachine/leetcode*/
class Solution {
public:
void connect(TreeLinkNode *root) {
while(root != nullptr){
TreeLinkNode *pre(nullptr), *next(nullptr);
for(; root != nullptr; root = root->next){
if(next == nullptr) next = root->left != nullptr ? root->left : root->right;
if(root->left != nullptr){
if(pre != nullptr) pre->next = root->left;
pre = root->left;
}
if(root->right != nullptr){
if(pre != nullptr) pre->next = root->right;
pre = root->right;
}
}
root = next;
}
}
};




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: