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. PowerShell 4 on win7 sp1

    https://www.microsoft.com/en-hk/download/details.aspx?id=40855 文件太多了,按照这个http://stackoverflow.com/qu ...

  2. eclipse注释模板

    设置注释模板的入口: Window->Preference->Java->Code Style->Code Template 然后展开Comments节点就是所有需设置注释的元 ...

  3. PE结构笔记

    提示:前面加*为必须背下来的 DOS头: typedef struct _IMAGE_DOS_HEADER { // DOS .EXE header WORD e_magic; //* Magic n ...

  4. CA*Layer(CAReplicatorLayer--)

    CAReplicatorLayer (反射应用) 指定一个继承于UIView的ReflectionView,它会自动产生内容的反射效果: + (Class)layerClass//我们也可以通过重写V ...

  5. So easy Webservice 7.CXF 发布WebService

    (一)使用ServerFactoryBean 方式实现发布WS服务 1.新建项目,添加cxf jar包到项目中 2.编写服务实现类 /** * CXF WebService * 不用注解 * @aut ...

  6. 《Linux内核设计的艺术》学习笔记(一)从开机加电到加载三个汇编源码

      实验内核版本:0.11 ◆ 从开机到main函数的三步: ① 启动BIOS,准备实模式下的中断向量表和中断服务程序: ② 从启动盘加载OS程序到内存中,加载OS程序的工作就是利用第一步中的中断服务 ...

  7. 移动端图表插件jChart.js的修改

    bug1: 折线图,设置datasetGesture : true时,Y轴的刻度值居然会变.会变也就算了,居然没地方设置不能变. bug2: 折线图,设置tap.point事件,和datasetGes ...

  8. NYOJ214

    单调递增子序列(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 给定一整型数列{a1,a2...,an}(0<n<=100000),找出单调递增最长 ...

  9. 个人博客 week1

    1:看完教材后不懂的几个问题: (1):团队合作中每个人分工而行,如何保证比较完善的各人之间的信息共享? (2):敏捷开发如何保证开发的效率?即在进行大项目的开发时敏捷开发看上去会很混乱. (3):黑 ...

  10. Maven仓库构建

    什么是Maven仓库 在不用Maven的时候,比如说以前我们用Ant构建项目,在项目目录下,往往会看到一个名为/lib的子目录,那里存放着各类第三方依赖jar文件,如log4j.jar,junit.j ...