每次应该把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. CSDN也有我的博客啦

    我的CSDN:https://blog.csdn.net/qq_40875849

  2. [VS2013]常见异常修正

    未能加载文件或程序集“Microsoft.SqlServer.Management.Sdk.Sfc, Version=11.0.0.0, Culture=neutral, PublicKeyToken ...

  3. solusvm安装过程

    openvz solusvm ** Testing connectivity PING solusvm.com (69.168.233.94) 56(84) bytes of data. 64 byt ...

  4. [UE4]虚幻UE4 .uproject文件无关联 右键菜单少了

    前一段时间因为一些事,重装系统  然后重新安装UE4跟VS  ,突然发现...竟然之前的UE4原先的项目找不到了,然后用UE4打开就提示 “该文件没有与之关联的程序来执行该操作,请先安装一个程序... ...

  5. Socket的长连接和短连接

    讨论Socket必讨论长连接和短连接 一.长连接和短连接的概念 1.长连接与短连接的概念:前者是整个通讯过程,客户端和服务端只用一个Socket对象,长期保持Socket的连接:后者是每次请求,都新建 ...

  6. Access、SQLServer、Oracle常见SQL语句应用区别

    Access.SQLServer.Oracle常见SQL语句应用区别 关劲松 PMP 如果要兼容Access.SQL Server.Oracle三个数据库版本:我们在编写SQL语句的过程中,尽量使用一 ...

  7. Oracle安装完成后如何创建表空间及用户

    1.select file_Name from dba_data_files;(查询表空间) 2.create tablespace QUAN datafile '/app/ADMINISTRATOR ...

  8. CRM 2016 一个IFrame页面,执行另一IFrame页面的函数

    如果IFrame_A  想执行 IFrame_B 中 RefreshSelf() 函数,可以按以下方法写: 此js代码写在 IFrame_A 中 parent.Xrm.Page.getControl( ...

  9. delphi版本对应

    delphi 7 delphi 8delphi 2005 ----- 9delphi 2006 ----- 10 delphi 2007 ----- 11delphi 2009 ----- 12 de ...

  10. MySQL字符集及校对规则的理解

      阅读目录:MySQL的字符集和校对规则 MySQL的字符集 MySQL与字符集 正确使用字符集 MySQL客户端与字符集 字符集编码转换原理 字符集常见处理操作 字符集的正确实践 MySQL的校对 ...