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 ...
随机推荐
- [转][JS]修改链接中的参数
转自:https://blog.csdn.net/weixin_40845192/article/details/81561644 /** * url地址修改 * @param url 待修改url ...
- jQuery 事件的命名空间简单了解
原文地址:http://www.jb51.net/article/43626.htm 用 jQuery 绑定和解绑事件监听器都是非常简单的,怎样精确地解绑其中一个监听器?我们需要了解一下事件的命名 ...
- sqlserver创建表
--创建学员信息数据表 use StudentManageDB go if exists(select * from sysobjects where name='Students') drop ta ...
- springmvc前端控制器的三种拦截方式
*.do :只拦截.do文件 / :拦截除jsp页面的所有请求,包括restful类型的url /* :拦截所有请求包括jsp页面
- MPEG-1视屏压缩标准
MPEG-1标准包括5个部分 图像的四种类型: I帧: B帧:双向帧间预测 P帧: D帧:只含有16分量,为快放设计 压缩前需要帧重排 视屏码流结构 I帧压缩 p帧压缩 b帧压缩 其他压缩算法 MPE ...
- C#写Excel(OleDB)
先辟谣(至少对Excel2010来说) IMEX ( IMport EXport mode )设置 IMEX 有三种模式,各自引起的读写行为也不同,容後再述: 0 is Export mode:只能写 ...
- 用vlan实现同一网段的的各部门之间有的可以通信有的不可以通信
日前老师上课演示一个项目:实现公司同一网段的各个部门之间有的可以通信有的无法通信.我们用的是思科测试软件模拟操作,个人觉得很好用. 在刚开始做这个项目的时候我以为端口是对应的,如图,交换机 ...
- CPU Rings, Privilege, and Protection.CPU的运行环, 特权级与保护
原文标题:CPU Rings, Privilege, and Protection 原文地址:http://duartes.org/gustavo/blog/ [注:本人水平有限,只好挑一些国外高手的 ...
- python第三方库,你要的这里都有
Python的第三方库多的超出我的想象. python 第三方模块 转 https://github.com/masterpy/zwpy_lst Chardet,字符编码探测器,可以自动检测文本. ...
- oracle常用加解密函数
md5 CREATE OR REPLACE FUNCTION MD5( passwd IN VARCHAR2) RETURN VARCHAR2 IS retval varchar2(32); BEGI ...