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".

  1. /**
  2. * Definition for binary tree with next pointer.
  3. * struct TreeLinkNode {
  4. * int val;
  5. * TreeLinkNode *left, *right, *next;
  6. * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. void connect(TreeLinkNode *root) {
  12. // Start typing your C/C++ solution below
  13. // DO NOT write int main() function
  14. if(root == NULL)
  15. return;
  16. if(root->left == NULL && root->right == NULL){
  17. root->next = NULL;
  18. return;
  19. }
  20.  
  21. vector<TreeLinkNode *> nodes_previous_level;
  22. nodes_previous_level.push_back(root);
  23.  
  24. while(true) {
  25. vector<TreeLinkNode *> nodes_current_level;
  26. for(auto item: nodes_previous_level) {
  27. if(item->left != NULL)
  28. nodes_current_level.push_back(item->left);
  29. if(item->right != NULL)
  30. nodes_current_level.push_back(item->right);
  31. }
  32.  
  33. if(nodes_current_level.size() == )
  34. break;
  35.  
  36. //connect
  37. for(int j =; j< nodes_current_level.size(); j++) {
  38. if(j+ == nodes_current_level.size())
  39. nodes_current_level[j]->next = NULL;
  40. else
  41. nodes_current_level[j]->next = nodes_current_level[j + ];
  42.  
  43. }
  44.  
  45. nodes_previous_level.clear();
  46. nodes_previous_level = nodes_current_level;
  47. }
  48.  
  49. return;
  50. }
  51. };

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. BP神经网络求解异或问题(Python实现)

    反向传播算法(Back Propagation)分二步进行,即正向传播和反向传播.这两个过程简述如下: 1.正向传播 输入的样本从输入层经过隐单元一层一层进行处理,传向输出层:在逐层处理的过程中.在输 ...

  2. 【面向打野编程】——KMP算法入门

    一.问题 咱们先不管什么KMP,来看看怎么匹配两个字符串. 问题:给定两个字符串,求第二个字符串是否包含于第一个字符串中. 为了具体化,我们以 ABCAXABCABCABX 与 ABCABCABX为例 ...

  3. STORM_0008_Structure-of-the-codebase_Storm的代码库的结构

    http://storm.apache.org/releases/1.0.1/Structure-of-the-codebase.html Structure of the codebase 源码分成 ...

  4. 九度-剑指Offer

    二维数组中的查找 分析:既然已经给定了每一行从左至右递增,那么对于每一行直接二分查找即可,一开始还想着每一列同样查找一次,后来发现每一行查找一遍就能够遍历所有的元素了. #include <cs ...

  5. JAX-WS:背后的技术JAXB及传递Map

    转载:http://www.programgo.com/article/98912703200/ 1.什么是JAX-WS JAX-WS (JavaTM API for XML-Based Web Se ...

  6. Promise A 规范的一个简单的浏览器端实现

    简单的实现了一个promise 的规范,留着接下来模块使用.感觉还有很多能优化的地方,有时间看看源码,或者其他大神的代码 主要是Then 函数.回调有点绕人. !(function(win) { fu ...

  7. 07 concurrency and Multi-version

    本章提要---------------------------------------------------------对并发和锁的进一步补充并发控制事务的隔离级别多版本控制读一致性的含义写一致性- ...

  8. mysql 性能问题的解决

    场景:模拟一天的数据,每个10秒,遍历1000个设备,每个设备模拟一个实时数据,总的数据量为:24*60*60/10*1000 = 864万条记录.-------------------------- ...

  9. Docker-创建支持ssh服务的镜像

    这里测试tomcat镜像安装ssh服务 1.启动镜像 [root@wls12c docker]$ docker run -d tomcat:centos 844bdde121a03174f3abd22 ...

  10. Python标准库06 子进程 (subprocess包)

    这里的内容以Linux进程基础和Linux文本流为基础.subprocess包主要功能是执行外部的命令和程序.比如说,我需要使用wget下载文件.我在Python中调用wget程序.从这个意义上来说, ...