题目来源:

  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. 素数距离问题_ny_24.java

    素数距离问题 时间限制: 3000 ms  |  内存限制: 65535 KB 难度: 2   描述 现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度.如果左右有等距 ...

  2. k8s 集群基本概念

    一.概述: kubernetes是google开源的容器集群管理系统,提供应用部署.维护.扩展机制等功能,利用kubernetes能方便管理跨集群运行容器化的应用,简称:k8s(k与s之间有8个字母) ...

  3. 用js获取周、月第一天和最后一天(转载)

    var getCurrentWeek = function (day) { var days = ["周日", "周一", "周二", &q ...

  4. VlanTrunk

    简单的vlan trunk的配置: 第一步:添加vlan 1 Switch>enable 2 Switch#show vlan VLAN Name Status Ports ---- ----- ...

  5. ASP.NET MVC+Entity Framework 4.1访问数据库

    Entity Framework 4.1支持代码优先(code first)编程模式:即可以先创建模型类,然后通过配置在EF4.1下动态生成数据库. 下面演示两种情形: 1.代码优先模式下,asp.n ...

  6. 浅谈Mybatis(一)

    一.MyBatis引言 1.基本概念 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google co ...

  7. Vijos 1493 传纸条

    此题,刚开始看上去以为是加简单的动态规划,但是写了后,交上去发自现不对.后来在网上查了题解后发现用到了“多线程DP”的东西.这种DP就是用来解决这种问题的.和P1143 三取方格数那道题很像.只不过是 ...

  8. 创建一个ROS包

    先前笔者不知道catkin到底是个什么东东,后来终于在官方网站上找到了答案,原来catkin是ROS的一个官方的编译构建系统,是原本的ROS的编译构建系统rosbuild的后继者.catkin的来源有 ...

  9. Struts2知识总结

    整篇参考:http://blog.csdn.net/zq9017197/article/details/5958627 要搞清楚以下几点: 1.Struts2是什么?它的运行原理是什么? 2.Stru ...

  10. (4)事件处理——(1)事件处理(Handling Events)

    JavaScript has several built-in ways of reacting to user interaction and other events. To make a pag ...