题目来源:

  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. FilenameUtils工具类

    转载请出自出处:http://eksliang.iteye.com/blog/2217081 一.概述 这是一个Java操作文件的常用库,是Apache对java的IO包的封装,这里面有两个非常核心的 ...

  2. SQL学习之用通配符进行数据过滤

    一.Like操作符 之前介绍的所有的操作符都是针对已知值进行过滤.不管匹配一个值还是多个值,检验大于还是小于已知值,或者检查某个范围的值,其共同点是过滤中使用的值都是已知的. 但是这种方法并不是任何时 ...

  3. Ext.Net 使用总结之查询条件中的起始日期

    2.关于查询条件中起始日期的布局方式 首先上一张图,来展示一下我的查询条件的布局,如下: 大多数时候,我们的查询条件都是一个条件占一个格子,但也有不同的时候,如:查询条件是起始日期,则需要将这两个条件 ...

  4. css3动画工具

    去年,我刚刚开始学习css3时候,看到了腾讯的这个工具,引起了我对css3的兴趣. 配合着书本上的知识写了一些效果,感觉不错. http://www.f2e.name/case/css3/tools. ...

  5. jQuery Mobile基础

    1.安装 在<head></head>标签里边写入以下内容 jQuery Mobile CDN: <head> <meta charset="utf ...

  6. 关于SVM一篇比较全介绍的博文

    转自:http://blog.csdn.net/v_july_v/article/details/7624837 支持向量机通俗导论(理解SVM的三层境界) 前言 动笔写这个支持向量机(support ...

  7. iOS开发之C语言函数库

    在iOS开发中不可避免的需要使用数学函数进行计算,我们可以使用C标准库头文件中定义的数学常量宏及数学函数来进行基本的数学计算操作,这些C函数都可以在微软的MSDNAPI库中找到.(https://ms ...

  8. java下拉框,滚动条

    package com.soft.test; /** * 下拉列表.下拉框.滚动条的使用 */ import javax.swing.*; import java.awt.*; public clas ...

  9. BootstrapTable+KnockoutJS

    BootstrapTable+KnockoutJS实现增删改查解决方案(三):两个Viewmodel搞定增删改查   前言:之前博主分享过knockoutJS和BootstrapTable的一些基础用 ...

  10. css3_note

    css3基础 css3选择器 属性选择器 属性选择器基本上IE7+都支持,可以放心的使用,参见caniuse [attr] [attr=val] [attr*=val] [attr^=val] [at ...