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 ...
随机推荐
- MVVM模式下关闭窗口的实现
通过行为来实现 实现界面与逻辑的分离 窗口关闭行为:其中含有布尔型的Close属性,将相应的关闭行为绑定到该属性上,则可以实现窗口的关闭行为,从而实现VM与View的分离 public class W ...
- C语言sscanf和sprintf输入输出使用及Strlen、Memset解释
sscanf() - 从一个字符串中读进与指定格式相符的数据. swscanf()- 用于处理宽字符字符串,和sscanf功能相同 通过学习和使用个人认为,在字符串格式不是很复杂,但是也并不 ...
- Java_Certificates does not conform to algorithm constraints
java.security.cert.CertificateException: Certificates does not conform to algorithm constraints SSL证 ...
- slf4j 和 log4j合用的(Maven)配置
简述: 添加logger的日志输出,下面是配置信息供备忘 步骤: 1. 在Maven的porn.xml 文件中添加dependency如下 <dependency> <group ...
- @Transactional导致AbstractRoutingDataSource动态数据源无法切换的解决办法
上午花了大半天排查一个多数据源主从切换的问题,记录一下: 背景: 项目的数据库采用了读写分离多数据源,采用AOP进行拦截,利用ThreadLocal及AbstractRoutingDataSource ...
- 两个div如何并列 (转)
两个div如何并列?当用到div+css代替table时,我习惯用两个方法: 1 <div id="parent"> <div id="child_1& ...
- DMA/TIM capture
This is a more free standing example measuring the LSI (TIM5_CH4 internally) and demonstrating DMA/T ...
- DNS服务器
DNS服务器是指“域名解析服务器”,而域名就是我们通常所说的“网址”.在互联网中识别和寻找不同的计算机,实际上是需要知道该计算机的IP地址才能进行访问.比如220.181.38.4,这个IP就是百度的 ...
- .Net Discovery 系列之六--深入浅出.Net实时编译机制(下)
接上文 在初始化时,HashTable中各个方法指向的并不是对应的内存入口地址,而是一个JIT预编译代理,这个函数负责将方法编译为本地代码.注意,这里JIT还没有进行编译,只是建立了方法表! 下表(表 ...
- 淘宝应对"双11"的技术架构分析
原文地址:http://kb.cnblogs.com/page/193670/ 双“11”最热门的话题是TB ,最近正好和阿里的一个朋友聊淘宝的技术架构,发现很多有意思的地方,分享一下他们的解析资料: ...