LeetCode 题解:Populating Next Right Pointers in Each Node I & II 二有难度。考虑不全面。
每次应该把root同层的右侧节点传过来。如果没有,就传NULL。
同时,应该是先右后左。
感觉这次的代码还挺简洁的。。
void construct(struct TreeLinkNode *root, struct TreeLinkNode *r_brother)
{
if(root == NULL) return;
root->next = r_brother;
construct(root->right,r_brother == NULL ? NULL : r_brother->left);
construct(root->left, root->right);
}
void connect(struct TreeLinkNode *root) {
construct(root,NULL);
}
题目II,第一次提交错了。因为r_brother本身没有儿子,而他右侧又有儿子的情况,没有考虑到。
比如下图中的 7 节点,因为5没有儿子,按照之前的逻辑,7也没有右兄弟。
这时应该怎么办呢?
应该沿着5,向右挨个询问,直到最右,都没有儿子,才死心。

void construct(struct TreeLinkNode *root, struct TreeLinkNode * r_brother)
{
if(root == NULL) return; root->next = r_brother; struct TreeLinkNode * new_r_brother = NULL; while(r_brother != NULL)
{
new_r_brother = r_brother->left == NULL ? r_brother->right : r_brother->left;
if(new_r_brother) break; r_brother = r_brother->next;
} if(root->right)
{
construct (root->right, new_r_brother);
new_r_brother = root->right;
}
construct(root->left, new_r_brother);
}
void connect(struct TreeLinkNode *root) {
construct(root,NULL);
}
LeetCode 题解:Populating Next Right Pointers in Each Node I & II 二有难度。考虑不全面。的更多相关文章
- 【leetcode】Populating Next Right Pointers in Each Node I & II(middle)
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- 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 ...
- [LeetCode] 117. Populating Next Right Pointers in Each Node II 每个节点的右向指针 II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- [Leetcode Week15]Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...
- [Leetcode Week15]Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/populati ...
- LeetCode:Populating Next Right Pointers in Each Node I II
LeetCode:Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeL ...
- 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 ...
- 【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树
题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * Le ...
随机推荐
- 06-ICMP: Internet 控制报文协议
I C M P经常被认为是I P层的一个组成部分.它传递差错报文以及其他需要注意的信息. I C M P报文通常被I P层或更高层协议( T C P或U D P)使用.一些I C M P报文把差错报文 ...
- 2018年最新PHP面试题
面试之前多看看公司的资料,可以看出面试的公司主要做什么,电商,数据库,php函数,sql的优化,接口,session和cookie等经常会问到,都是必问之题,这其中有一部分题目摘抄自网络,回答也不错 ...
- CentOS6系统防火墙开启、关闭、查看状态(转载)
原文:https://blog.csdn.net/aaron_80726/article/details/79027760 注意:要进入到~目录 也就是家目录下才能查看防火墙 进入家目录:cd ~ 关 ...
- python中文件操作
打印进度条
- Java 问题集
1.命令行编译.java文件,找不到或者无法加载主类,需要配置完整的PATH,CLASSPATH环境变量,CLASSPATH最前面是 点+分号 PATH=%JAVA_HOME%\binCLASSPAT ...
- sas 批量处理缺少缺失值
DATA S.customer_grade; SET S.customer_grade; ARRAY NUM{*} _NUMERIC_; DO I=1 TO DIM(NUM); ...
- java 两个日期之间的天数
private static int numDays(String start,String end){ Calendar startCal=Calendar.getInstance(); Strin ...
- SSH 项目整合
SSH整合:spring + springmvc + hibernate 1.1 生成Maven项目:ar_ssh 1.2 添加jar包:pom.xml 与ssm相比,主要添加了spring与hibe ...
- 图片Alpha预乘的作用[转]
Premultiplied Alpha 这个概念做游戏开发的人都不会不知道.Xcode 的工程选项里有一项 Compress PNG Files,会对 PNG 进行 Premultiplied Alp ...
- 网页的缓存Cache与控制
什么是缓存 Cache? 缓存位于客户端与服务器之间, 或者服务器与服务器之间.它决定是否保存所获资源的副本,以及如何使用副本,何时更新副本,这里所说的资源包括页面的HTML, 图片,文件等等. 使用 ...