#Leet Code# Populating Next Right Pointers in Each Node II
描述:注意需要先self.connect(right)再self.connect(left),否则会有case通不过,原因是左边递归执行时依赖与右边的next已经建立,而先执行connect(left)的话右边还没有完成关系的建立。
代码:
class Solution:
# @param root, a tree node
# @return nothing
def doSth(self, nextNode, conNode):
while nextNode is not None:
if nextNode.left is None and nextNode.right is None:
nextNode = nextNode.next
elif nextNode.left is not None:
conNode.next = nextNode.left
break
else:
conNode.next = nextNode.right
break def connect(self, root):
if root is None:
return if root.left is None and root.right is None:
return
elif root.left is None and root.right is not None:
self.doSth(root.next, root.right)
elif root.left is not None and root.right is None:
self.doSth(root.next, root.left)
else:
root.left.next = root.right
self.doSth(root.next, root.right) self.connect(root.right)
self.connect(root.left)
#Leet Code# 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
题目链接: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】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 ...
- [Leetcode Week15]Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...
随机推荐
- hdu 1039 Easier Done Than Said? 字符串
Easier Done Than Said? Time Limi ...
- Spring.NET学习笔记
http://www.cnblogs.com/GoodHelper/archive/2009/11/20/SpringNet_Index.html
- SilkTest天龙八部系列4-ChildWin
一直觉得SilkTest的ChildWin不好理解,今天大致看了一下,其实它和DialogBox每啥区别,也是一种window类型.帮助里面说,典型的ChildWin就是文档窗口.在Windows上, ...
- 得到当前堆栈信息的两种方式(Thread和Throwable)的纠结
今天进行slf4j中logger的同步封装.主要目的是为了以后方便更换日志实现系统. 遇到的问题:使用Thread.currentThread().getStackTrace()[1].getClas ...
- stack计算表达式的值
9.52 使用stack对象处理带圆括号的表达式.遇到左圆括号时,将其标记下来.当你在一个左括号之后遇到右圆括号时,弹出stack对象中这两边括号之间的元素,直到遇到左括号,将左括号也一起弹出栈. 接 ...
- 动作-CCActionInterval之CCActionEase家族
补间动作也是一个包装器(之前叫补间动画,从字面上讲,叫动作更合适一些.).你也可以叫他缓释动作. 1.含义 补间动作改变的是,内部动作的执行速率(注意,并没有改变执行的最终效果,和执行的时间.)关于这 ...
- Linux 释放cached内存
使用free -m 查看系统使用的内存情况: # free -m total used free shared buffers ...
- IEnumerable 和 IQueryable
共有两组 LINQ 标准查询运算符,一组在类型为 IEnumerable<T> 的对象上运行,另一组在类型为 IQueryable<T> 的对象上运行.构成每组运算符的方法分别 ...
- C#操控条形码扫描枪
// 条码扫描器 // 窗体部分相关代码: using System; using System.Collections.Generic; using System.ComponentModel; u ...
- Sql Server 与CLR集成
.NET编程和SQL Server ——Sql Server 与CLR集成 一.SQL Server 为什么要与CLR集成 1. SQL Server 提供的存储过程.函数等十分有限,经常需要外部 ...