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:/ ...
随机推荐
- IOS编程User Interface基础
IOS编程之User Interface基础 目录 概述 相关概念 常见问题 状态栏的隐藏 应用图标的设置 概述 IOS用户界面是APP呈现给用户最直观.最常用的方式,因此学会用户界面的编程是学习IO ...
- innodb 变量
http://dev.mysql.com/doc/refman/5.7/en/innodb-parameters.html
- redis的实现过程
1下载redis的安装包并按照操作安装 2开启 右击我的电脑→管理→服务→站到redis service服务 将其开启 注意:redis服务开启后其默认的ip和端口号为127.0.0.1:6379 3 ...
- Linux修改用户组
usermod -g group loginname 强行设置某个用户所在组 usermod -G groups loginname 把某个用户改为 group(s) usermod -a -G gr ...
- HTTP Referer二三事---转
授权方式:署名,非商业用途,保持一致,转载时请务必以超链接(http://www.fwolf.com/blog/post/320)的形式标明文章原始出处和作者信息及本声明. 什么是HTTP Refer ...
- Java再学习——synchronized与volatile
volatile:只保证共享资源的可见性的,任何修改都写在主存,所有线程马上就能看到,适用于新值不依赖于旧值的情形. synchronized:保证可操作的原子性一致性和可见性. volatile和s ...
- C#实现万年历(农历、节气、节日、星座、属相、生肖、闰年等)
C# 万年历 农历 节气 节日 星座 星宿 属相 生肖 闰年月 时辰等,代码如下: using System.Collections.Generic; using System.Text; using ...
- 1.6.3 Uploading Data with Solr Cell using Apache Tika
1. Uploading Data with Solr Cell using Apache Tika solr使用Apache Tika工程的代码提供了一个框架,用于合并所有不同格式的文件解析器为so ...
- 自定义EditText实现一键删除数据
转载请注明出处http://blog.csdn.net/xiaanming/article/details/11066685 自定义EditText带删除小图标, 实现的功能: 点击删除小图标,删除当 ...
- PHP trim()函数的一些用法
string trim ( string $str [, string $charlist ] ) - 去除字符串首尾处的空白字符(或者其他字符) trim()函数当第二个参数为空时,默认去掉空格.制 ...