[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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
随机推荐
- [LOJ#2980][THUSCH2017]大魔法师(线段树+矩阵)
每个线段树维护一个行向量[A,B,C,len]分别是这个区间的A,B,C区间和与区间长度,转移显然. 以及此题卡常,稍微哪里写丑了就能100->45. #include<cstdio> ...
- 【线性基】Petrozavodsk Winter Training Camp 2018 Day 1: Jagiellonian U Contest, Tuesday, January 30, 2018 Problem A. XOR
题意:给你一些数,问你是否能够将它们划分成两个集合,使得这两个集合的异或和之差的绝对值最小. 设所有数的异或和为S,集合A的异或和为A. 首先,S的0的位对答案不造成影响. S的最高位1,所对应的A的 ...
- nginx与Lua执行顺序
Nginx顺序 Nginx 处理每一个用户请求时,都是按照若干个不同阶段(phase)依次处理的,而不是根据配置文件上的顺序. Nginx 处理请求的过程一共划分为 11 个阶段,按照执行顺序依次是 ...
- [JOISC2014]水筒
OJ题号: BZOJ4242.AtCoder-JOISC2014E 题目大意: 给你一个h*w的网格图,每个格子可能是空地.障碍物和建筑物. 你只可以从空地上走过或者从建筑物中穿过. 建筑物总共有p个 ...
- python 爬虫 处理超级课程表传输的数据
借鉴的别人的思路 http://www.oschina.net/code/snippet_2463131_53711 抓取超级课程表传输的数据 他的传输数据居然是明文的-- 现在已经把自己的课表都抓出 ...
- js取float型小数点后x位数的方法
js中取小数点后两位方法最常用的就是四舍五入函数了,前面我介绍过js中四舍五入一此常用函数,这里正好用上,下面我们一起来看取float型小数点后两位一些方法总结 以下我们将为大家介绍 JavaScri ...
- Xcode更新后插件失效解决办法
defaults read /Applications/Xcode.app/Contents/Info DVTPlugInCompatibilityUUID 获取新版xcode的uuid Xcode6 ...
- Windows下ftp服务器搭建及配置
Win系统使用ser-u软件进行FTP服务器的搭建下载地址:https://www.serv-u.com/操作步骤如下:1. 点击执行程序进行按照SU-FTP-Server-Windows-v15.1 ...
- centos7安装maven
下载maven 下载地址:http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.3.9/binaries/apache-maven-3. ...
- NGINX如何反向代理Tomcat并且实现Session保持
简介 LNMT=Linux+Nginx+MySQL+Tomcat: Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器: 在中小型系统和并发访问用户不是很多的场合下被 ...