[LeetCode]题解(python):117-Populating Next Right Pointers in Each Node II
题目来源:
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的更多相关文章
- 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)
[LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 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 笔记 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】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] 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 ...
- 【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 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 ...
- 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 ...
- 117. Populating Next Right Pointers in Each Node II
题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...
- 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 ...
随机推荐
- swift论坛正式上线
www.iswifting.com swift论坛正式上线.有问答专区,也有技术分享区,还有学习资料区,大家一起学习成长! 2014中国互联网大会于8月26日开幕. 政府主管部门.行业专家.企业领袖将 ...
- (转)Web开发中最致命的小错误
Web开发中最致命的小错误 现在,有越来越多所谓的“教程”来帮助我们提高网站的易用性.本文收集了一些在 Web 开发中容易出错和被忽略的小问题,并且提供了参考的解决方案,以便于帮助 Web 开发者更好 ...
- MVC多表联合查询数据显示
随然做过几年.net开发,但一直没有做过MVC框架下的网站,这段时间无事,学习一下.下面的方法是我摸索过程中的一点总结,如果有更好的方法,欢迎告诉我,谢谢. 这段时间我只看了MVC和LinQ两本书,关 ...
- 阿里P8分享:关于做事方式与做事态度
转载:http://www.neitui.me/y/1019 阿里P8分享:关于做事方式与做事态度贴图1: 贴图2: 贴图3:
- English - 定冠词和不定冠词(a an the) 的区别
不定冠词表示泛指,定冠词表示特指. 不定冠词a (an)与数词one 同源,是"一个"的意思.a用于辅音音素前,一般读作[e],而an则用于元音音素前,一般读做[en]. 1) 表 ...
- Truncate Delete 用法
Truncate /Delete Table 1.含义上都是删除表全部记录 2.Truncate 是属于数据定义语言,系统不会写每一笔记录操作事务日志,无法恢复记录数据的操作 Truncate Ta ...
- iOS实践01
去年放假之前大概完成了新浪微博项目,到现在也忘得差不多了,打算在重新写一遍.之前的一些笔记在新浪的博客SleenXiu,在这主要是把新浪微博以随笔的形式写在这,方便以后的复习. 先看看之前主要完成的几 ...
- css3图片3D翻转效果
点击图片看翻转效果 html结构 <div class="flip"> <div class="front"> <img src= ...
- halcon与C#混合编程
halcon源程序: dev_open_window(0, 0, 512, 512, 'black', WindowHandle)read_image (Image, 'C:/Users/BadGuy ...
- 转: git常用命令
# git配置 #---------------------------------------------- #配置用户名和邮箱: $ git config --global user.name & ...