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:/ ...
随机推荐
- Xcode常见的编译、运行等错误的解决
Xcode常见的编译.运行等错误的解决 项目没找到Info.plist的错误 The solution for this particular instance of the error was “I ...
- [置顶] HashMap HashTable HashSet区别剖析
HashMap.HashSet.HashTable之间的区别是Java程序员的一个常见面试题目,在此仅以此博客记录,并深入源代码进行分析: 在分析之前,先将其区别列于下面 1:HashSet底层采用的 ...
- 关于解决Permission is only granted to system apps
一句话,clean一下这个Project!就OK了…… 不要被假象迷惑!
- 使用Thread类可以创建和控制线程
1.创建线程 static void Main(string[] args) { /* Thread类 * 创建控制线程 * 其构造函数接受ThreadStart和ParameterizedThrea ...
- myecplise 打开报错 Errors occurred during the build. Errors running builder 'DeploymentBuilder' on project 'myf'. Java.lang.NullPointerException
Errors occurred during the build. Errors running builder 'DeploymentBuilder' on project 'myf'.Java.l ...
- Solr DIH dataconfig配置
1. 配置文件data-config.xml定义了数据库的基本配置,以及导出数据的映射规则,即导出数据库表中对应哪些字段的值,以及对特定字段的值做如何处理 </pre><p>& ...
- Android(java)学习笔记61:多线程程序的引入
- 判断某个对象是不是DOM对象
在写js代码时有时需要判断某个对象是不是DOM对象,然后再进行后续的操作,这里我给出一种兼容各大浏览器,同时又算是比较稳妥的一种方法. 要判断一个对象是否DOM对象,首先想到的无非就是它是否具有DOM ...
- IQueryable与IEnumberable的区别
IEnumerable接口 公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代.也就是说:实现了此接口的object,就可以直接使用foreach遍历此object: IQueryable 接口 ...
- 解决Elementary OS和Windows时间不一致
解决方案一:把Windows从硬件时间修改成修改成UTC时间,管理员身份运行 Reg add HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInforma ...