首页 > 代码库 > Leetcode#116 Populating Next Right Pointers in Each Node
Leetcode#116 Populating Next Right Pointers in Each Node
原题地址
Populating Next Right Pointers in Each Node II(参见这篇文章)的简化版,代码不变
代码:
1 void connect(TreeLinkNode *root) { 2 queue<TreeLinkNode *> layer; 3 4 layer.push(root); 5 while (!layer.empty()) { 6 queue<TreeLinkNode *> nextLayer; 7 TreeLinkNode *prev = NULL; 8 9 while (!layer.empty()) {10 TreeLinkNode *front = layer.front();11 layer.pop();12 if (!front)13 continue;14 if (prev)15 prev->next = front;16 prev = front;17 nextLayer.push(front->left);18 nextLayer.push(front->right);19 }20 layer = nextLayer;21 }22 }
Leetcode#116 Populating Next Right Pointers in Each Node
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。