Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II

时间:2022-10-04 13:27:50

题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ

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

Tags: Depth-first Search

分析

本题的解法完全可以用于Populating Next Right Pointers in Each Node。

本题的一个难点在于要求使用常数空间,这样的话普通的深度优先遍历由于需要递归压栈而无法使用,普通的广度优先搜索需要使用队列也无法使用,因此选择使用两层迭代,使用current指向当前结点的方法进行广度优先遍历。

同之前的简化版本不同,本题的区别在于二叉树并不完整,之前算法中可以将当前结点的右子结点与下一结点的左子结点相连的方法失效。因此这里选用广度优先遍历,引入“层”的概念,需要储存两个值:遍历的当前结点、本层上一个非空的结点(previous)。由于寻找遍历的下一个结点需要使用父层的next指针,因此算法设计为遍历当前结点时完成三件事:

  • 如果左子结点存在,则更新previous的next指向左子结点
  • 如果右子结点存在,则更新左子结点(或previous)的next指向右子结点
  • 下一次循环的当前结点设定为父节点的next的孩子

示例

class Solution:
# @param root, a tree node
# @return nothing
def connect(self, root):
head = None; # 下一层的首结点
previous = None; # 本层上一个非空的结点
current = root; # 当前结点 while current is not None:
while current is not None:
if current.left is not None:
if previous is not None:
previous.next = current.left;
else:
# previous为空,意味着此处是本层首结点
head = current.left;
previous = current.left;
if current.right is not None:
if previous is not None:
previous.next = current.right;
else:
head = current.right;
previous = current.right;
# 当前结点遍历完毕,转入父节点的next结点的子结点
current = current.next;
# 当前层遍历完毕,转入下一层
current = head;
head = None;
previous = None;

Leetcode 笔记系列的Python代码共享在https://github.com/wizcabbit/leetcode.solution

相关题目

Populating Next Right Pointers in Each Node

Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II的更多相关文章

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

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  2. 【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

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

  4. LeetCode OJ 117. Populating Next Right Pointers in Each Node II

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

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

  6. Leetcode 笔记 116 - Populating Next Right Pointers in Each Node

    题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...

  7. leetcode@ [116/117] Populating Next Right Pointers in Each Node I & II (Tree, BFS)

    https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ Follow up for problem ...

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

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

随机推荐

  1. Monkeyrunner脚本中component快速定位方法

    在编写MonkeyRunner脚本过程中,会出现component这一项内容,很多人可能不知道怎么确认,其实这个主要是为了指定要测试的程序包名和主Activity名,我们可以用以下方法去进行确认: 1 ...

  2. spring多线程与定时任务

    本篇主要描述一下spring的多线程的使用与定时任务的使用. 1.spring多线程任务的使用 spring通过任务执行器TaskExecutor来实现多线程与并发编程.通常使用ThreadPoolT ...

  3. C语言位运算详解(转载)

    转载自:http://www.cnblogs.com/911/archive/2008/05/20/1203477.html 位运算是指按二进制进行的运算.在系统软件中,常常需要处理二进制位的问题.C ...

  4. xlsx导入成--json

    这两天遇到大难题了,就是这个   xlsx   导入问题,之前用的xlsx.full.min.js,写的导入,结果不兼容ie浏览器,研究这个也好长时间,网上居然还没有搜到合适的,自己写从xlsx官网上 ...

  5. Unity的常用API

    1.Event Function:事件函数   Reset() :被附加脚本时.在游戏物体的组件上按Reset时会触发该事件函数 Start() :在游戏初始化时会执行一次 Update() :每一帧 ...

  6. 【相关网站 - 01】- Java 相关网站

    一.官方网站 1. Java 官方网站 https://www.java.com/zh_CN/ 2. Spring 官方网站 http://spring.io/ 1. Spring Framework ...

  7. redhat7.3 superset的离线安装

    superset是一个python 开发的可视化工具,可以与kylin连接进行数据分析,在官网的讲解中,采用了在线安装方式,生产环境中有yum源,但是没有网,不得不采用离线安装方式.(我们先在有网的环 ...

  8. [原][osg]osgconv浅析

    查看osgconv.cpp main函数在533行 osg::ArgumentParser arguments(&argc,argv); //........一堆功能不管,先看一下文件读写 F ...

  9. MyEclipse怎么导入导出项目

    MyEclipse怎么导入导出项目 | 浏览:25271 | 更新:2012-06-06 17:48 1 2 3 4 5 6 7 分步阅读 MyEclipse,是一个十分优秀的功能强大的JavaEE的 ...

  10. MySQL+Navicat for MySQL安装

    一.安装MySQL 1.下载MySQL http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.10-winx64.zip 2.安装 2.1解压安装包 ...