【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

标签: LeetCode


题目地址:https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/description/

题目描述:

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

题目大意

把一棵完全二叉树的每层节点之间顺序连接,形成单链表。

解题方法

【LeetCode】116. Populating Next Right Pointers in Each Node 解题报告(Python)很像,只不过这个题没有完全二叉树的条件,因此我们需要额外的条件。

下面这个做法没满足题目中的常数空间的要求,不过是个非递归的好做法,对完全二叉树也完全试用。做法就是把每层的节点放到一个队列里,把队列的每个元素进行弹出的时候,如果它不是该层的最后一个元素,那么把它指向队列中的后面的元素(不把后面的这个弹出)。

# Definition for binary tree with next pointer.
# class TreeLinkNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# self.next = None class Solution:
# @param root, a tree link node
# @return nothing
def connect(self, root):
if not root: return
queue = collections.deque()
queue.append(root)
while queue:
_len = len(queue)
for i in range(_len):
node = queue.popleft()
if i < _len - 1:
node.next = queue[0]
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)

方法二:

constant extra space.

待续。

日期

2018 年 3 月 14 日 –霍金去世日

【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)的更多相关文章

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

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

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

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

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

  5. Leetcode#117 Populating Next Right Pointers in Each Node II

    原题地址 二叉树的层次遍历. 对于每一层,依次把各节点连起来即可. 代码: void connect(TreeLinkNode *root) { if (!root) return; queue< ...

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

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

  9. [Leetcode Week15]Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...

随机推荐

  1. mysql 除法运算保留小数的用法

    说明:刚开始用的round(值1/值2*100,1) 结果没出效果,才搜到decimal函数 在工作中会遇到计算小数而且需要显现出小数末尾的0,我们会用到DECIMAL这个函数,这是一个函数非常强悍: ...

  2. 关于stm32不常用的中断,如何添加, 比如timer10 timer11等

    首先可以从keil中找到 比如找到定时器11的溢出中断,如上图是26 然后,配置定时器11 溢出中断的时候,我就在:下面填上这个变量. 之后要写中断服务函数,也就是发生中断后要跳转到的函数. 需要知道 ...

  3. Android项目的settings.gradle和build.gradle

    gradle构建的项目中的build.gradle和settings.gradle文件 build.gradle 浅析(一) 史上最全的Android build.gradle配置教程 Android ...

  4. C++之无子数

    题目如下: 1 #include <iostream> 2 3 using namespace std; 4 5 6 bool isThisNumhaveChild(int num); 7 ...

  5. AOP与IOC的概念

    AOP与IOC的概念(即spring的核心) a) IOC:Spring是开源框架,使用框架可以使我们减少工作量,提高工作效率并且它是分层结构,即相对应的层处理对应的业务逻辑,减少代码的耦合度.而sp ...

  6. recyclerView DiffUtil使用

    DiffUtil是和RecyclerView一块用的,DiffUtil用来比较两个数据集,他的最大用处是在RecyclerView刷新时,不在无脑. 以前adapter.notifyDataSetCh ...

  7. 使用 ACE 库框架在 UNIX 中开发高性能并发应用

    使用 ACE 库框架在 UNIX 中开发高性能并发应用来源:developerWorks 中国 作者:Arpan Sen ACE 开放源码工具包可以帮助开发人员创建健壮的可移植多线程应用程序.本文讨论 ...

  8. Linux(CentOS 7)使用gcc编译c,c++代码

    安装gcc: 1.使用 yum -list gcc* 查询 centos 官方gcc的所有包: 可安装的软件包 gcc.x86_64 gcc-c++.x86_64 gcc-gfortran.x86_6 ...

  9. Log4j 被曝核弹级漏洞,开发者炸锅了!

    大家好,我是鱼皮,开门见山,知名的开源项目 Apache Log4j 出事了! 2021 年 12 月 9 日,该项目被曝存在 严重安全漏洞 ,攻击者只需要向目标机传入一段特殊代码,就能触发漏洞,自由 ...

  10. centos配置 显示中文

    目录 一.简介 二.操作 一.简介 不显示中文,出现这个情况一般是由于没有安装中文语言包,或者设置的默认语言有问题导致的. 二.操作 1.查看当前系统语言 登陆linux系统打开操作终端之后,输入 e ...