每次应该把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. [转][C#]手写 Socket 服务端

    private void OpenSocket(int port) { Task.Factory.StartNew(() => { server = new Socket(AddressFami ...

  2. centos6.9安装oracle11g

    感谢强哥的文档 源文档链接 https://www.qstack.com.cn/archives/68.html #------------------------------------------ ...

  3. 控件之ReleLayout属性

    Android:控件布局(相对布局)RelativeLayout RelativeLayout是相对布局控件:以控件之间相对位置或相对父容器位置进行排列. 相对布局常用属性: 子类控件相对子类控件:值 ...

  4. Jmeter(三)Test-Plan、Thread-Group

    思量再三,还是再记一会,看到技术群里边的讨论,真的是压力山大,学习一刻也不能耽搁.继续来回顾Jmeter的知识吧. Test-Plan,是所有Jmeter脚本的根节点,Test-Plan中包含名称.注 ...

  5. win10家庭版怎么开启Administrator超级管理员帐户

    一.win10家庭版开启administrator方法: 1.通过Cortana搜索cmd,匹配出“命令提示符”,右键以管理员身份运行:   2.在打开的命令提示符窗口输入net user admin ...

  6. HBase 集群部署

     前提条件:hadoop及zookeeper机群已经搭建好. 配置hbase集群步骤: 1.配置hbase集群,要修改3个文件 注意:要把hadoop的hdfs-site.xml和core-site. ...

  7. tp5 计算两个日期之间相差的天数

    //两个日期之间相差的天数 function diffBetweenTwoDays ($day1, $day2) { $second1 = strtotime($day1); $second2 = s ...

  8. 用户禁止cookie后,如何继续使用session

    (1)如果用户禁止cookie,服务器仍会将sessionId以cookie的方式发送给浏览器,但是,浏览器不再保存这个cookie(即sessionId)了. (2)如果想继续使用session,需 ...

  9. vue实现点击、滑动右侧字母对应各个城市

    1.字母组件给父组件传递当前点击的字母值 @click="handleLetterClick" //绑定事件 handleLetterClick (e) { //向上传递参数 th ...

  10. idea error:Command line is too long

    今天在正在本地运行的项目中写了一个无关项目的测试类,执行main函数时报错如下: 解决方案: 找到项目根目录下的.idea/workspace.xml,添加内容: <component name ...