Problem Description http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/

Basic idea is to get every nodes in the current level by iterating nodes of the previous level, then to iterate all nodes in current level to connect them with pointer "next".

 /**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:
void connect(TreeLinkNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(root == NULL)
return;
if(root->left == NULL && root->right == NULL){
root->next = NULL;
return;
} vector<TreeLinkNode *> nodes_previous_level;
nodes_previous_level.push_back(root); while(true) {
vector<TreeLinkNode *> nodes_current_level;
for(auto item: nodes_previous_level) {
if(item->left != NULL)
nodes_current_level.push_back(item->left);
if(item->right != NULL)
nodes_current_level.push_back(item->right);
} if(nodes_current_level.size() == )
break; //connect
for(int j =; j< nodes_current_level.size(); j++) {
if(j+ == nodes_current_level.size())
nodes_current_level[j]->next = NULL;
else
nodes_current_level[j]->next = nodes_current_level[j + ]; } nodes_previous_level.clear();
nodes_previous_level = nodes_current_level;
} return;
}
};

Populating Next Right Pointers in Each Node II [Leetcode]的更多相关文章

  1. Populating Next Right Pointers in Each Node II leetcode java

    题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...

  2. Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II

    题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...

  3. 【leetcode】Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...

  4. 29. Populating Next Right Pointers in Each Node && Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node OJ: https://oj.leetcode.com/problems/populating-next-rig ...

  5. Populating Next Right Pointers in Each Node,Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node Total Accepted: 72323 Total Submissions: 199207 Difficul ...

  6. leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II

    leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  7. Leetcode 树 Populating Next Right Pointers in Each Node II

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...

  8. LeetCode: Populating Next Right Pointers in Each Node II 解题报告

    Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...

  9. 【LeetCode】117. Populating Next Right Pointers in Each Node II (2 solutions)

    Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...

随机推荐

  1. 【转载】COM 连接点

    原文:COM 连接点 CLR 完全介绍 COM 连接点 Thottam R. Sriram 来自:http://msdn.microsoft.com/zh-cn/magazine/cc163361.a ...

  2. VBA中自定义类和事件的(伪)注册

    想了解一下VBA中自定义类和事件,以及注册事件处理程序的方法. 折腾了大半天,觉得这样的方式实在称不上“注册”,所以加一个“伪”字.纯粹是瞎试,原理也还没有摸透.先留着,有时间再接着摸. 做以下尝试: ...

  3. VS生成事件

    源自:http://www.cnblogs.com/FreeDong/p/3406737.html 如果说磨刀不误砍柴工,同样用好Visual Studio,会大大增加咱.NET程序猿效率.本文说的就 ...

  4. 在CSS中,BOX的Padding属性的数值赋予顺序为

    4种可能的情况,举例说明: padding:10px; // 四个内边距都是10px padding:5px 10px; // 上下5px 左右10px padding:5px 10px 15px; ...

  5. VS中的路径宏 vc++中OutDir、ProjectDir、SolutionDir各种路径 转

    说明 $(RemoteMachine) 设置为“调试”属性页上“远程计算机”属性的值.有关更多信息,请参见更改用于 C/C++ 调试配置的项目设置. $(References) 以分号分隔的引用列表被 ...

  6. Android中的五大布局和logcat打印日志

    在android中的布局有五大类,有的时候你可能用到一种,但有的时候你也可能需要两种或者三种布局同时一起使用.这五种布局为别为:LinearLayout(线性布局),FrameLayout(框架布局) ...

  7. 如何将自己开发的标签打成jar包

    1: 在Myeclipse中新建一个java工程 2: 将你的标签处理器类统统都拷到工程里面, 将tld文件拷到META-INF里面 3:点击file里面的export,

  8. 《转》Ubuntu 12.04常用的快捷键

    Ubuntu 12.04常用的快捷键   超级键操作   1.超级键(Win键)–打开dash.   www.2cto.com   2.长按超级键– 启动Launcher.并快捷键列表.   3.按住 ...

  9. MySQL Server 5.7解压版缺少文件无法启动

    如题: 一般认为5.7中mysql目录下 缺少data/mysql/目录,导致无法启动: 解决方案: 重新安装5.6即可: 1.删除5.7中安装的服务: 到mysql\bin目录下运行:mysqld ...

  10. [转载] Tmux 速成教程:技巧和调整

    原文: http://blog.jobbole.com/87584/ 决定从 screen 转向 tmux 了, 非常喜欢 tmux 的窗格功能. 简介 有些开发者经常要使用终端控制台工作,导致最终打 ...