【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. find命令常见用法

    1. find linux中,find命令一般用来按特定条件查找文件,生产环境中也常用其来过滤文件 名称 find - 搜索目录层次结构中的文件 格式 find [目录] {[选项] [参数]}... ...

  2. 关于写SpringBoot+Mybatisplus+Shiro项目的经验分享一:简单介绍

    这次我尝试写一个原创的项目 the_game 框架选择: SpringBoot+Mybatisplus+Shiro 首先是简单的介绍(素材灵感来自英雄联盟) 5个关键的表: admin(管理员): l ...

  3. 使用Redis实现令牌桶算法

    在限流算法中有一种令牌桶算法,该算法可以应对短暂的突发流量,这对于现实环境中流量不怎么均匀的情况特别有用,不会频繁的触发限流,对调用方比较友好. 例如,当前限制10qps,大多数情况下不会超过此数量, ...

  4. adjust, administer

    adjust to just, exact. In measurement technology and metrology [度量衡学], calibration [校准] is the compa ...

  5. day08 文件属性

    day08 系统目录 今日内容 一.重要目录 1./usr 2./var 3./proc 二.文件的属性 1.文件属性的介绍 2.文件属性的详述 3.企业案例 /usr 安装第三方软件的目录: 1./ ...

  6. 字符串属性转变List属性存入数据库

    项目中有系统IP字段,现将string转List存入数据库,每个功能块持久层实现方法不一样(分为jpa和mp) jpa: @Convert(converter = JpaConverterListJs ...

  7. 技术预演blog

    canal整合springboot实现mysql数据实时同步到redis spring+mysql集成canal springboot整合canal监控mysql数据库 SpringBoot cana ...

  8. springboot+vue集成mavon-editor,开发在线文档知识库

    先睹为快,来看下效果: 技术选型 SpringBoot.Spring Security.Oauth2.Vue-element-admin 集成mavon-editor编辑器 安装 mavon-edit ...

  9. Linux下部署Java项目(jetty作为容器)常用脚本命令

    startup.sh #!/bin/bash echo $(basename $(pwd)) "jetty started" cd jetty nohup java -Xmx8g ...

  10. 常用 HTTP 状态码

    下面是列举的我在项目中用到过的一些 HTTP 状态码,当然,在具体的使用中并不是用到的状态码越多越好,需要结合自己项目情况来选用适合自己的 HTTP 状态码.   HTTP 状态码 含义说明 200 ...