Populating Next Right Pointers in Each Node II
题目地址: https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
关键思路:讲节点的左右子节点链接起来,遍历节点的next节点,即可。
class Solution {
public:
TreeLinkNode* gethead(TreeLinkNode* root)
{
if(root == NULL)
return NULL;
if(root->left != NULL)
return root->left;
else
return root->right;
}
TreeLinkNode* gettail(TreeLinkNode* root)
{
if(root == NULL)
return NULL;
if(root->right != NULL)
return root->right;
else
return root->left;
}
void connect(TreeLinkNode *root)
{
TreeLinkNode* head = NULL;
TreeLinkNode* tail = NULL;
if(root == NULL)
return;
TreeLinkNode* pcur = root;
while(root != NULL)
{
TreeLinkNode* headtmp = gethead(root);
TreeLinkNode* tailtmp = gettail(root);
if(headtmp == NULL)
{
root= root->next;
continue;
}
if(headtmp != tailtmp)
headtmp->next = tailtmp;
if(tail != NULL)
{
tail->next = headtmp;
tail = tailtmp;
}
else
{
head = headtmp;
tail = tailtmp;
}
root = root->next;
}
while(pcur != NULL)
{
TreeLinkNode* tmp = gethead(pcur);
if(tmp != NULL)
{
connect(tmp);
break;
}
pcur = pcur->next;
}
}
};
Populating Next Right Pointers in Each Node II的更多相关文章
- 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 ...
- [Leetcode Week15]Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...
- 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)
[LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
随机推荐
- 0c-41-ARC使用特点及注意事项
1.ARC特点总结 1)不允许调用release,retain,retainCount 2)允许重写dealloc,但是不允许调用[super dealloc] 3)@property的参数: str ...
- MySQL数据库InnoDB存储引擎中的锁机制
MySQL数据库InnoDB存储引擎中的锁机制 http://www.uml.org.cn/sjjm/201205302.asp 00 – 基本概念 当并发事务同时访问一个资源的时候,有可能 ...
- MYSQL 博客
DavidYang的博客 - CSDN.NET DimitriK's (dim) Weblog Xaprb · Stay Curious! 飞鸿无痕的博客 - ChinaUnix博客 何登成的技术博客 ...
- 基于sqlite的Qt 数据库封装
[代码] mydata.h 10 #ifndef MYDATA_H 11 #define MYDATA_H 12 #include <QObject> 13 #include <QS ...
- 去掉或者修改 input、select 等表单的【默认样式 】
隐藏input等表单的默认样式的背景: textarea,select,input{-webkit-appearance: none; -moz-appearance: none; -o-appear ...
- CSS 之 内层div填充margin,外层div的背景色不会覆盖该margin
外层元素(如div)中只有一个非空子元素,此时margin是被折叠了.两者之间取最大的margin值,表现在外层父元素上,而不是内层子元素. 注意: (1)只有垂直方向上才会出现此现象,水平方向不会出 ...
- K - Ancient Messages(dfs求联通块)
K - Ancient Messages Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Subm ...
- [经典算法] 字符串搜索Boyer-Moore
题目说明: 今日的一些高阶程式语言对于字串的处理支援越来越强大(例如Java.C#.Perl等),不过字串搜寻本身仍是个值得探讨的课题,在这边以Boyer- Moore法来说明如何进行字串说明,这个方 ...
- Swift - 使用NSNotificationCenter发送通知,接收通知
转载自:http://www.mamicode.com/info-detail-1069228.html 标签: 1,通知(NSNotification)介绍 这里所说的通知不是指发给用户看的通知消息 ...
- CSS3实战手册(第3版)(影印版)
<CSS3实战手册(第3版)(影印版)> 基本信息 原书名:CSS3: The Missing Manual, 3E 作者: David Sawyer McFarland 出版社:东南大学 ...