描述:注意需要先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的更多相关文章

  1. LeetCode: Populating Next Right Pointers in Each Node II 解题报告

    Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...

  2. 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 ...

  3. 【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 ...

  4. 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 ...

  5. 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 ...

  6. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  7. Leetcode 树 Populating Next Right Pointers in Each Node II

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...

  8. 【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 ...

  9. [Leetcode Week15]Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...

随机推荐

  1. codeforces 132C Logo Turtle--- dp dfs

    题目在这里:点击打开链接 题意: F表示前进一步,T表示变成反方向 给一串FT字符,和一个n,表示可以改变多少次,求可以走到的离原点最远的距离 改变就是F变成T.T变成F 关键: dfs(int d, ...

  2. Delphi 调用批处理

    uses ShellApi; procedure TForm1.Button1Click(Sender: TObject); var filename, Path: string; begin fil ...

  3. Objective C 代码片段(类别)

    @interface NSString (reverse) -(NSString *) reverseString; @end @implementation NSString (reverse) - ...

  4. spring mvc[转]

    Spring 注解学习手札(一) 构建简单Web应用 Spring 注解学习手札(二) 控制层梳理 Spring 注解学习手札(三) 表单页面处理 Spring 注解学习手札(四) 持久层浅析 Spr ...

  5. [Ember] Creating Your First Ember.js Project with Ember-CLI

    In this lesson, we'll setup Ember-CLI and use it to create and run our first Ember.js project. Insta ...

  6. 【转】如何高效利用GitHub——2013-08-28 22

    http://www.yangzhiping.com/tech/github.html  正是Github,让社会化编程成为现实.本文尝试谈谈GitHub的文化.技巧与影响. Q1:GitHub是什么 ...

  7. react 组件开发

    参考资料 https://toddmotto.com/react-create-class-versus-component/ React-Native的代码规范,React其实也基本一样~~ htt ...

  8. Android App用MulticastSocket监听组播,为什么连接到不同路由、在不同手机上跑,有的能收到有的收不到

    ---------------------------!! 转载请注明出处 !!-----------------------   一个项目,利用wifi组播在局域网内发现设备.却发现在有的路由器上能 ...

  9. Form开发中组件控制的几个常用方法

    转自:http://oracleseeker.com/2009/09/01/graphical_component_control_in_oracle_ebs_form/ 在Oracle EBS 的F ...

  10. OpenSSH Client信息泄露和缓冲区溢出漏洞

    一.风险简述: 2016年1月14日OpenSSH发布官方公告称,OpenSSH Client 5.4~7.1版本中未公开说明的功能(Roaming)存在信息泄漏和缓冲区溢出漏洞,此漏洞可能导致您通过 ...