Populating Next Right Pointers in Each Node II [Leetcode]
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]的更多相关文章
- 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 ...
- 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 ...
- 【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 ...
- 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 ...
- 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 ...
- 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- Leetcode 树 Populating Next Right Pointers in Each Node II
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...
- LeetCode: Populating Next Right Pointers in Each Node II 解题报告
Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...
- 【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 ...
随机推荐
- Cheatsheet: 2013 09.01 ~ 09.09
.NET Multi Threaded WebScraping in CSharpDotNetTech .NET Asynchronous Patterns An Overview of Projec ...
- 【转载】CMake 简介和 CMake 模板
转载自我的博客: CMake 简介和 CMake 模板 . 如果你用 Linux 操作系统,使用 cmake 会简单很多,可以参考一个很好的教程: CMake 入门实战 | HaHack .如果你用 ...
- 502 Proxy Error The proxy server received an invalid response from an upstream server
Proxy Error The proxy server received an invalid response from an upstream server. The proxy server ...
- 清除SVN获取文件的所有.svn文件夹
Windows Registry Editor Version 5.00[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\清除SVN信息] @=&qu ...
- hdu 1800 (map)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 Flying to the Mars Time Limit: 5000/1000 MS (Java/ ...
- C++指针内存
这是一个关于C++指针的问题,思考了一下 void GetMemory(char *p, int num){ p = (char*) malloc (sizeof(char) * num); } vo ...
- equals()和hashcode()
默认调用的情况: 1.集合在存放对象时,首先判断hashcode(),再判断equals如果都是true,认为是相同的两个元素不进行存储. 删除对象时,将从hashcode指定位置查找再删除 2.在h ...
- [转载] 360分布式存储系统Bada的设计和应用
原文: http://mp.weixin.qq.com/s?__biz=MzAwMDU1MTE1OQ==&mid=208931479&idx=1&sn=1dc6ea4fa28a ...
- 疯狂java讲义之流程控制与数组
while package ch4; /** * Created by Jiqing on 2016/11/6. */ public class While { public static void ...
- Maven clean时候出现异常
首先我使用IDEA创建一个空的project,在这个空的project中创建了一个maven module,然后将这个module打包之后,使用maven clean这个target 的时候报错,如下 ...