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. window--窗口

    创建窗口 1. 通过标签窗口窗口. <div id="win" class="easyui-window" title="My Window&q ...

  2. ServiceStack.OrmLite 笔记9 -code first 必须的代码优先

    复杂点的使用3 code first的使用,支持复杂类型 public enum PhoneType { Home, Work, Mobile, } public enum AddressType { ...

  3. adb shell input keyevent code详解

    adb shell input keyevent 7 # for key '0' adb shell input keyevent 8 # for key '1' adb shell input ke ...

  4. 网上搜集的jq常用代码

    1. 设置IE特有的功能:  if ($.browser.msie) { //do something... } 2. 使用jQuery来代替一个元素: $('#thatdiv').replaceWi ...

  5. 第二篇,MVC框架

    MVC : 模型(model)---视图(view)---控制器(caontreller)的缩写 MVC是一个设计模式,他强制性的使应用程序的输入,处理,输出 分开 模型(Model):程序员编写程序 ...

  6. MySQL数据表生成ER图

    遇到没有说明文档的数据库的时候,一张一张表去分析,需要花费很长时间和精力. 幸好有一些工具,可以帮助我们生成ER图,这样看起来就一目了然: 下面我将自己的一次实践记录于下,供参考: 1.下载并安装工具 ...

  7. V8 引擎的sort算法

    用的是快速排序,有点小问题 function ArraySort(comparefn) { // In-place QuickSort algorithm. // For short (length ...

  8. As Easy As Possible

    题目链接 题意:一个只含e, a, s, y的字符串,问[l, r]内有多少个easy序列? 题解:倍增. #include <bits/stdc++.h> using namespace ...

  9. 机器学习如何选择模型 & 机器学习与数据挖掘区别 & 深度学习科普

    今天看到这篇文章里面提到如何选择模型,觉得非常好,单独写在这里. 更多的机器学习实战可以看这篇文章:http://www.cnblogs.com/charlesblc/p/6159187.html 另 ...

  10. gevent中如何实现长轮询

    浏览网页时,浏览器会传HTTP 请求到服务器,服务器会根据请求将网页的内容传给浏览器,但是在很多的情况下,使用者会需要看到最新的即时性资讯,例如观看股票市场行情,而在以前只能靠着重新载入网页才能获得最 ...