题目来源:

  https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/


题意分析:

  根据上一题,如果给定的树不是完全树同层的连接。要求常数空间时间复杂度。如:

         1
/ \
2 3
/ \ \
4 5 7
结果是:
         1 -> NULL
/ \
2 -> 3 -> NULL
/ \ \
4-> 5 -> 7 -> NULL

题目思路:

  用两个指针来记录下一个指针和下一层第一个指针。


代码(python):

  

 # Definition for binary tree with next pointer.
# class TreeLinkNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# self.next = None class Solution(object):
def connect(self, root):
"""
:type root: TreeLinkNode
:rtype: nothing
"""
if root:
tmp,tmp1,tmp2 = root,None,None
while tmp:
if tmp.left:
if tmp1:
tmp1.next = tmp.left
tmp1 = tmp.left
if not tmp2:
tmp2 = tmp1
if tmp.right:
if tmp1:
tmp1.next = tmp.right
tmp1 = tmp.right
if not tmp2:
tmp2 = tmp1
tmp = tmp.next
self.connect(tmp2)

[LeetCode]题解(python):117-Populating Next Right Pointers in Each Node II的更多相关文章

  1. 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

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

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

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

  6. 【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

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

  8. leetcode 117 Populating Next Right Pointers in Each Node II ----- java

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  9. 117. Populating Next Right Pointers in Each Node II

    题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...

  10. 117. Populating Next Right Pointers in Each Node II (Tree; WFS)

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

随机推荐

  1. Android @+id与@id的区别

      Android中的组件需要用一个int类型的值来表示,这个值也就是组件标签中的id属性值.id属性只能接受资源类型的值,也就是必须以@开头的值,例如,@id/abc.@+id/xyz等. 如果在@ ...

  2. URL中增加BASE64加密的字符串引起的问题(java.net.MalformedURLException:Illegal character in URL)

    序 昨天在做一个 Demo 的时候,因为是调用第三方的接口,採用的是 HTTP 的通信协议,依照文档上的说明,须要把參数进行加密后增加到 URL 中.可是,就是这个看似普普通通的操作,却让我着实费了非 ...

  3. iOS 使用Method Swizzling隐藏Status Bar

    在iOS 6中,隐藏Status Bar很的简单. // iOS 6及曾经,隐藏状态栏 [[UIApplication sharedApplication] setStatusBarHidden:YE ...

  4. 虚拟键盘,移动web开发的痛

    原生APP的弹出虚拟键盘和收回虚拟键盘,输入框始终贴在虚拟键盘的上方.如下图: 如果移动端web也想做类似的功能,可以实现吗?  你可能会说着:“不就是放一个position: fixed;的输入框在 ...

  5. [Puzzle] 蚂蚁路线碰撞问题

    有这么一道题目, 看下面的图, 假设有一条直线, 每个叉叉上有一只蚂蚁, 它们会随机选择一个方向, 向前或者向后移动, 每次走一格, 前进中当两只蚂蚁相遇, 它们会掉头, 问: 全部蚂蚁都走出去的最长 ...

  6. BZOJ 1969: [Ahoi2005]LANE 航线规划( 树链剖分 )

    首先我们要时光倒流, 倒着做, 变成加边操作维护关键边. 先随意搞出一颗树, 树上每条边都是关键边(因为是树, 去掉就不连通了)....然后加边(u, v)时, 路径(u, v)上的所有边都变成非关键 ...

  7. 关于Connection must be valid and open.

    这个Bug真心很操蛋! 我的网站在公司做的运行一切都没问题,回家后咋自己的电脑上出现了Connection must be valid and open.这个问题. 我最后还是在英文网站的一个不起眼的 ...

  8. 获取新浪天气api显示天气情况(转)

    直接上一个html的demo <!doctype html> <html class="no-js fixed-layout"> <head> ...

  9. 手机端 UI一些插件

    手机弹出框 http://yun.baidu.com/share/link?shareid=3523128425&uk=2685891615

  10. Ubuntu Server 14.04在VMware安装的一些事儿

    这几天一直在折腾Ubuntu Server 14.04,故记录下: 安装前的准备: 1.建议安装英文版,像15.04.16.04等安装中文版时存在bug,而且中文版字体显示也有问题. 2.Ubuntu ...