Populating Next Right Pointers in Each Node II leetcode java
题目:
Follow up for problem "Populating Next Right Pointers in Each Node".
What if the given tree could be any binary tree? Would your previous solution still work?
Note:
- You may only use constant extra space.
For example,
Given the following binary tree,
1
/ \
2 3
/ \ \
4 5 7
After calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ \
4-> 5 -> 7 -> NULL 题解:
这道题跟I的区别就是binary tree不是完全二叉树。
所以root.right.next就不一定等于root.next.left。
所以,目标就是先确定好root的右孩子的第一个有效next连接点,然后再处理左孩子。 代码如下:
1 public void connect(TreeLinkNode root) {
2 if (root == null)
3 return;
4
5 TreeLinkNode p = root.next;
6 /*
7 因此,这道题目首要是找到右孩子的第一个有效的next链接节点,然后再处理左孩子。然后依次递归处理右孩子,左孩子
8 */
9 while (p != null) {
if (p.left != null) {
p = p.left;
break;
}
if (p.right != null) {
p = p.right;
break;
}
p = p.next;
}
if (root.right != null) {
root.right.next = p;
}
if (root.left != null) {
if(root.right!=null)
root.left.next = root.right;
else
root.left.next = p;
}
connect(root.right);
connect(root.left);
}
Populating Next Right Pointers in Each Node II leetcode java的更多相关文章
- Populating Next Right Pointers in Each Node II [Leetcode]
Problem Description http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ ...
- Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II
题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...
- 【leetcode】Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
- 29. Populating Next Right Pointers in Each Node && Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node OJ: https://oj.leetcode.com/problems/populating-next-rig ...
- Populating Next Right Pointers in Each Node,Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node Total Accepted: 72323 Total Submissions: 199207 Difficul ...
- 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- Leetcode 树 Populating Next Right Pointers in Each Node II
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...
- LeetCode: Populating Next Right Pointers in Each Node II 解题报告
Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...
- 【LeetCode】117. Populating Next Right Pointers in Each Node II (2 solutions)
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
随机推荐
- java string 替换img标签 正则表达式 任意多个字符
正则表达式 任意多个字符 (.*) 正则表达式中,“.”(点符号)匹配的是除了换行符“\n”以外的所有字符 要匹配包括 '\n' 在内的任何字符,([\s\S]*) 也可以用 “([\d\D]*)” ...
- bzoj3628: [JLOI2014]天天酷跑
题目链接 bzoj3628: [JLOI2014]天天酷跑 题解 开始读错题目了,尴尬 由于题目说的跳跃次数和高度是在一开始设定的. 发现枚举一下记忆化搜索就可以过了 要注意,跳到最高点是可以不下坠继 ...
- (android高仿系列)今日头条 --新闻阅读器 (转载)
非常不错,原文地址:http://blog.csdn.net/vipzjyno1/article/details/26514543
- java集合系列之LinkList
概要 第1部分 LinkedList介绍第2部分 LinkedList数据结构第3部分 LinkedList源码解析(基于JDK1.6.0_45) 第5部分 LinkedList示例 转载请注明出处 ...
- 第一次使用autohotkey的记录
第一次使用autohotkey的记录 原来想着直接用python来做模拟输入的,后面查了一下发现,目前的封装的库不一定能支持输入到游戏里,是的,我是打算用来做游戏辅助的,嘿嘿嘿 暂时来讲,我只是看完了 ...
- Linux学习笔记07—mysql的配置
一.mysql简介 说到数据库,我们大多想到的是关系型数据库,比如mysql.oracle.sqlserver等等,这些数据库软件在windows上安装都非常的方便,在Linux上如果要安装数据库,咱 ...
- Tesseract ocr 3.02学习记录一
光学字符识别(OCR,Optical Character Recognition)是指对文本资料进行扫描,然后对图像文件进行分析处理,获取文字及版面信息的过程.OCR技术非常专业,一般多是印刷.打印行 ...
- 正确识别希捷Backup Plus新睿品1TB正品,杜绝奸商猖獗
刚刚在百度希捷贴吧发了此贴, 马上被删除, 无奈只能发于个人博客, 望看到的朋友能转载到"合适"位置,让更多的朋友看到. 避免上当. 最近准备买个移动硬盘备份电脑资料,看上了睿品 ...
- win8、server 2012 清除winsxs文件夹
使用系统自带的文件清理工具 1.组合键win+x ,选择“命令提示符(管理员)” 复制“dism /online /Cleanup-Image /StartComponentCleanup” 到dos ...
- EF Code First更新数据库时报错:provider: SQL Network Interfaces, error: 26
在使用EF Code First更新数据库时报如下错误: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Serv ...