【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 Pointers in Each Node".
What if the given tree could be any binary tree? Would your previous solution still work?
Note:
- You may only use constant extra space.
For example,
Given the following binary tree,
1
/ \
2 3
/ \ \
4 5 7
After calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ \
4-> 5 -> 7 -> NULL
解法一:
先使用队列进行BFS(O(n)),队列中的节点还需要存放当前所在层数。
如果前一个遍历到的节点pre与当前遍历到的节点cur不在同一层,那么pre->next需要指向NULL
如果在同一层,那么pre->next = cur
/**
* 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) {}
* };
*/
struct Node
{
TreeLinkNode* tree;
int level;
Node(TreeLinkNode* root, int l):tree(root),level(l) {}
}; class Solution {
public:
queue<Node*> q;
void connect(TreeLinkNode *root)
{
if(root == NULL)
return; Node* rootNode = new Node(root, );
q.push(rootNode); Node *cur = rootNode;
Node *pre = rootNode;
Node *temp = rootNode; while(!q.empty())
{
temp = q.front();
q.pop(); if(temp->tree->left)
{
Node* leftNode = new Node(temp->tree->left, temp->level+);
q.push(leftNode);
pre = cur;
cur = leftNode;
if(pre->level < cur->level)
//get to next level
pre->tree->next = NULL;
else
//still same level
pre->tree->next = cur->tree;
}
if(temp->tree->right)
{
Node* rightNode = new Node(temp->tree->right, temp->level+);
q.push(rightNode);
pre = cur;
cur = rightNode;
if(pre->level < cur->level)
//get to next level
pre->tree->next = NULL;
else
//still same level
pre->tree->next = cur->tree;
}
}
}
};

解法二:
稍作分析就可以发现,我们没有必要像BFS那样存储整个一层的节点。
只需要保留三个信息:下一层的头levelHead,本层的上一个节点prev,本层的当前节点cur即可
对于每一层来说,prev的next连接cur。当本层遍历完,通过levelHead进入下一层。
/**
* 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)
{
TreeLinkNode* cur = root; //node being visited
TreeLinkNode* levelHead = NULL; //first node in the next level
TreeLinkNode* prev = NULL; //last visited node while(cur)
{//there remains levels to be visited
while(cur)
{//there remains nodes to be visited in this level
if(cur->left)
{
if(prev)
{//cur->left is not the levelHead, link it to the prev
prev->next = cur->left;
}
else
{//cur->left is the levelHead
levelHead = cur->left;
}
prev = cur->left;
}
if(cur->right)
{
if(prev)
{//cur->right is not the levelHead, link it to the prev
prev->next = cur->right;
}
else
{//cur->right is the levelHead
levelHead = cur->right;
}
prev = cur->right;
}
cur = cur->next; //travel through this level
} cur = levelHead; //travel to the next level
levelHead = NULL;
prev = NULL;
}
}
};

【LeetCode】117. Populating Next Right Pointers in Each Node II (2 solutions)的更多相关文章
- 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)
[LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 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】115. Populating Next Right Pointers in Each Node (2 solutions)
Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...
- 【LeetCode】116. Populating Next Right Pointers in Each Node
题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode ...
- LeetCode OJ 117. Populating Next Right Pointers in Each Node II
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- 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@ [116/117] Populating Next Right Pointers in Each Node I & II (Tree, BFS)
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ Follow up for problem ...
- Java for LeetCode 117 Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
随机推荐
- Codeforces Round #303 (Div. 2) B. Equidistant String 水题
B. Equidistant String Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/54 ...
- 线程,线程安全与python的GIL锁
今天看到一篇文章,讲述的是几个提升python性能的项目:传送门 在看的过程中,接触到一个名词,一个从学python开始就一直看到,但是从来都是一知半解的名词,心里不开心,必须把它搞明白,对了,这个词 ...
- [转]web服务器apache架构与原理 &apache 监控
web服务器 在开始了解Apache前,我 ...
- Object-C编程基础总结:
1,nil,NULL,NSNull:nil用来给对象附值,object—c里允许对象为空,空对象也可以接受消息.但是不允许指针为空,NULL是给任何指针附值的.所以NULL只在C或C++里才用.NSN ...
- (转)SQL Server创建索引
什么是索引拿汉语字典的目录页(索引)打比方:正如汉语字典中的汉字按页存放一样,SQL Server中的数据记录也是按页存放的,每页容量一般为4K .为了加快查找的速度,汉语字(词)典一般都有按拼音.笔 ...
- .net开发Ae释放com对象的问题
本文转载自: http://www.cnblogs.com/yhlx125/archive/2011/11/22/2258543.html#2269154我的博文 http://www.cnblogs ...
- N个富文本编辑器/基于Web的HTML编辑器
转自:http://www.cnblogs.com/lingyuan/archive/2010/11/15/1877447.html 基于WEB的HTML 编辑器,WYSIWYG所见即所得的编辑器,或 ...
- 支持向量机通俗导论(理解SVM的三层境界) by v_JULY_v
支持向量机通俗导论(理解SVM的三层境界) 前言 动笔写这个支持向量机(support vector machine)是费了不少劲和困难的,原因很简单,一者这个东西本身就并不好懂,要深入学习和研究下去 ...
- thinkphp输出表格
//这是打印5列n行的表格,所以mod="5" value="4" <tr> <volist name="data" id ...
- 获取easyui calendar 属性
<div id="cc" class="easyui-calendar" style="width:250px;height:250px;&qu ...