原题地址: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的更多相关文章

  1. LeetCode: Populating Next Right Pointers in Each Node II 解题报告

    Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...

  2. [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 ...

  3. 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 ...

  4. LeetCode - Populating Next Right Pointers in Each Node II

    题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...

  5. [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 ...

  6. 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 ...

  7. 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 ...

  8. [LeetCode] Populating Next Right Pointers in Each Node 每个节点的右向指针

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  9. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

随机推荐

  1. 20169211《Linux内核原理与分析》第六周作业

    1.教材内容总结 2.实验报告 3.学习总结 一.教材内容总结 1.系统调用与应用编程接口API的区别 操作系统为用户态进程与硬件设备进行交互提供了一组接口,就是系统调用.它主要有一下三个方面的作用: ...

  2. 【运维理论】RAID级别简介

    独立硬盘冗余阵列(RAID, Redundant Array of Independent Disks),旧称廉价磁盘冗余阵列(RAID, Redundant Array of Inexpensive ...

  3. Win10如何配置Jdk环境变量

    对于每一位做Java开发的朋友来说,Jdk是必须要安装的,安装好了Jdk,其实并没有结束,还需要配置Jdk的环境变量,系统在不断地更新,小编给大家介绍一下如何在Win10下配置Jdk,并检测是否配置成 ...

  4. mmcrfs

    mmcrfs command Creates a GPFS™ file system. Synopsis mmcrfs Device {"DiskDesc[;DiskDesc...]&quo ...

  5. ARM 中必须明白的几个概念

    文章具体介绍了关于ARM的22个常用概念. 1.ARM中一些常见英文缩写解释 MSB:最高有效位: LSB:最低有效位: AHB:先进的高性能总线: VPB:连接片内外设功能的VLSI外设总线: EM ...

  6. window.open()/剪切板ZeroClipboard

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Centos 安装 WPS

    Linux有自己的一套类是Office的办公软件:LibreOffice,但是不是很友好,幸好WPS有Linux版本. 安装步骤: 1.安装依赖包 yum install libpng12 yum i ...

  8. CF1060E Sergey and Subway 思维

    分两种情况讨论 一种为奇数长为$L$的路径,在经过变化后,我们需要走$\frac{L}{2} + 1$步 一种为偶数长为$L$的路径,在变化后,我们需要走$\frac{L}{2}$步 那么,我们只需要 ...

  9. UOJ.87.mx的仙人掌(圆方树 虚树)(未AC)

    题目链接 本代码10分(感觉速度还行..). 建圆方树,预处理一些东西.对询问建虚树. 对于虚树上的圆点直接做:对于方点特判,枚举其所有儿子,如果子节点不在该方点代表的环中,跳到那个点并更新其val, ...

  10. Git 工具的使用,windows平台安装

    先谈谈版本控制的一些事 如果你严肃对待编程,就必定会使用"版本控制系统"(Version Control System). 随着信息科技的发展,软件开发已不是小手工作坊,软件的规模 ...