每次应该把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 二有难度。考虑不全面。的更多相关文章

  1. 【leetcode】Populating Next Right Pointers in Each Node I & II(middle)

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  2. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  3. 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 ...

  4. [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 ...

  5. [Leetcode Week15]Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...

  6. [Leetcode Week15]Populating Next Right Pointers in Each Node

    Populating Next Right Pointers in Each Node 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/populati ...

  7. 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 ...

  8. 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 ...

  9. 【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树

    题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * Le ...

随机推荐

  1. Python——字符串2.0(实验)(python programming)

    直接打s,是程序员看到的:打print(),是用户看到的 列表 ] #列表索引,与数组唯一不同:等号左端可修改 转载自:https://www.cnblogs.com/wwwwwei/p/104816 ...

  2. Win7 无法访问Installer服务

    还原系统后,卸载程序时,系统提示"无法访问Windows Installer服务" 一. 可能原因: msi.dll相关的组件未注册: 未开启windows installer服务 ...

  3. VLAN IEEE802.1Q

    一. VLAN产生原因-广播风暴 传统的局域网使用的是HUB,HUB只有一根总线,一根总线就是一个冲突域.所以传统的局域网是一个扁平的网络,一个局域网属于同一个冲突域.任何一台主机发出的报文都会被同一 ...

  4. [UE4]移动相机,使用Arrow组件来标记移动位置

    一.创建一个Arrow组件来标记要移动的位置(Arrow的用法之一就是用来标注坐标). 二.使用TimeLine时间轴结合插值Lerp来移动相机

  5. Webbrowser指定IE内核版本(更改注册表)

    如果电脑上安装了IE8或者之后版本的IE浏览器,Webbrowser控件会使用IE7兼容模式来显示网页内容.解决方法是在注册表中为你的进程指定引用IE的版本号. 比如我的程序叫做a.exe 对于32位 ...

  6. C,Java,C#数据类型对比总结

  7. CS229 6.12 Neurons Networks from self-taught learning to deep network

    self-taught learning 在特征提取方面完全是用的无监督的方法,对于有标记的数据,可以结合有监督学习来对上述方法得到的参数进行微调,从而得到一个更加准确的参数a. 在self-taug ...

  8. 配置MySQL GTID(Global Transaction IDs)复制

    一.GTID的简介 1.GTID的概述 .全局事物标识:global transaction identifieds. .GTID事物是全局唯一性的,且一个事务对应一个GTID. .一个GTID在一个 ...

  9. vue使用animate.css库

    //引入库 <link rel="stylesheet" type="text/css" href="animate.css"> ...

  10. Vbox共享文件夹不显示了

    博主之前装的虚拟机没啥问题,按部就班,打开“我的电脑”可以看到主机上的共享文件夹,最近重装了一波,各种问题就来了,包括共享文件夹设置好后,看不见了. 介绍比较麻烦的方案,就是打开“我的电脑”,在地址栏 ...