[leetcode]Populating Next Right Pointers in Each Node II @ Python
原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
题意:
Follow up for problem "Populating Next Right Pointers in Each Node".
What if the given tree could be any binary tree? Would your previous solution still work?
Note:
- You may only use constant extra space.
 
For example,
Given the following binary tree,
1
/ \
2 3
/ \ \
4 5 7
After calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ \
4-> 5 -> 7 -> NULL
解题思路:和"Populating Next Right Pointers in Each Node"这道题不同的一点是,这道题的二叉树不是满的二叉树,有些节点是没有的。但是也可以按照递归的思路来完成。在编写递归的基准情况时需要将细节都考虑清楚:
代码一:
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# self.next = None class Solution:
# @param root, a tree node
# @return nothing
def connect(self, root):
if root:
if root.left and root.right:
root.left.next = root.right
tmp = root.next
while tmp:
if tmp.left: root.right.next = tmp.left; break
if tmp.right: root.right.next = tmp.right; break
tmp = tmp.next
elif root.left:
tmp = root.next
while tmp:
if tmp.left: root.left.next = tmp.left; break
if tmp.right: root.left.next = tmp.right; break
tmp = tmp.next
elif root.right:
tmp = root.next
while tmp:
if tmp.left: root.right.next = tmp.left; break
if tmp.right: root.right.next = tmp.right; break
tmp = tmp.next
self.connect(root.right)
self.connect(root.left)
# @connect(root.right)should be the first!!!
代码二:
思路更加精巧,代码更加简洁。
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# self.next = None class Solution:
# @param root, a tree node
# @return nothing
def connect(self, root):
if root:
p = root; q = None; nextNode = None
while p:
if p.left:
if q: q.next = p.left
q = p.left
if nextNode == None: nextNode = q
if p.right:
if q: q.next = p.right
q = p.right
if nextNode == None: nextNode = q
p = p.next
self.connect(nextNode)
[leetcode]Populating Next Right Pointers in Each Node II @ Python的更多相关文章
- LeetCode: Populating Next Right Pointers in Each Node II 解题报告
		
Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...
 - [LeetCode] 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——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 - Populating Next Right Pointers in Each Node II
		
题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...
 - [LeetCode] [LeetCode] 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:Populating Next Right Pointers in Each Node I II
		
LeetCode:Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeL ...
 - 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] Populating Next Right Pointers in Each Node 每个节点的右向指针
		
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
 - 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
 
随机推荐
- Jquery的方法(一)
			
一.文档操作1.内部插入:append(),appendTo(),prepend():2.外部插入:after(),before():3.删除操作:remove(),empty():4.克隆操作:cl ...
 - python列表中中文编码的问题
			
在python2列表中,有时候,想打印一个列表,会出现如下显示: 这个是由于: print一个对象,是输出其“为了给人(最终用户)阅读”而设计的输出形式,那么字符串中的转义字符需要转出来,而且 也不要 ...
 - 每日踩坑 2018-06-19 AutoMapper简单性能测试
			
想使用 AutoMapper 类库来做一些映射到 DTO 对象的操作 但既然类似这样的类库内部是用反射来实现的,那么会比较在意性能. 所以来简单测试一下性能. 关于测试结果呢 emmmm 我是比较吃惊 ...
 - 【10.9校内练习赛】【搜索】【2-sat】【树链剖分】【A_star k短路】【差分约束+判负环】
			
在洛谷上复制的题目! P3154 [CQOI2009]循环赛 题目描述 n队伍比赛,每两支队伍比赛一次,平1胜3负0. 给出队伍的最终得分,求多少种可能的分数表. 输入输出格式 输入格式: 第一行包含 ...
 - Python开发_python的安装
			
Python几乎可以在任何平台下运行,如我们所熟悉的:Windows/Unix/Linux/Macintosh. 在这里我们说一下,在Windows操作系统中安装python. 我的操作系统为:Win ...
 - 记一次centos7.2下用crontab执行定时任务的过程(初级)
			
实验目的:每分钟往某个文件写数据(crontab最小单位是分钟),具体shell命令我是放在一个文件里的.先创建两个空文件:/tmp/a.txt(目标文件)和/tmp/a.sh(脚本文件). 命令如下 ...
 - MYSQL-5.5.37-win32.msi  这个版本得程序包谁有吗  可以给我一下吗?
			
之前下载了这个版本得mysql 但是跟服务器链接不上 后来我就卸载了 但由于卸载不干净 现在又删了注册表 好像把这个程序包得什么文件删除了 现在提示配置文件错误 所以有这个程序包得 ...
 - CareerCup之1.3字符串去重
			
[题目] 原文: 1.3 Design an algorithm and write code to remove the duplicate characters in a string witho ...
 - ios 得到目录大小 进率是1000
			
- (CGFloat)folderSizeAtPath:(NSString *) folderPath { NSFileManager * manager = [NSFileManager d ...
 - GlobalGetAtomName GlobalDeleteAtom 引用  WinAPI: AddAtom、DeleteAtom、FindAtom、GetAtomName、GlobalAddAtom、GlobalDeleteAtom、GlobalFindAtom、GlobalGetAtomName
			
http://www.cnblogs.com/del/archive/2008/02/28/1085124.html 这是储存字符串的一组 API.通过 AddAtom 储存一个字符串, 返回一个 I ...