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

LeetCode 116. Populating Next Right Pointers in Each Node

2014-08-08 06:55 417 查看
LeetCode 102. Binary Tree Level Order Traversal差不多个意思。

用q维护同一高度的节点,先进先出进行操作即可。

代码:

class Solution
{
public:
void connect(TreeLinkNode *root)
{
queue<TreeLinkNode*> nodes;
queue<TreeLinkNode*> children;
nodes.push(root);
nodes.push(NULL);
while ( nodes.front() != NULL )
{
auto cur = nodes.front();
nodes.pop();
cur->next = nodes.front();
if (cur->left != NULL)
{
children.push( cur->left );
}
if (cur->right != NULL)
{
children.push( cur->right );
}
if (nodes.front() == NULL)
{
nodes = children;
nodes.push(NULL);
while (children.empty() == false)
{
children.pop();
}
}
}
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LeetCode C++