[抄题]:

Given a binary tree

struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *next;
}

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

Initially, all next pointers are set to NULL.

Note:

  • You may only use constant extra space.
  • Recursive approach is fine, implicit stack space does not count as extra space for this problem.

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道递归的关系:递归recursion分成遍历traverse一个人走,dc分治法两个人走

[一句话思路]:

利用不写公式的递归,先单层遍历,再多层遍历

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 已经声明过,就开辟过空间,不需要再次声明。但是可以赋值。
  2. 同一层每个节点都要算,所以curr = curr.next;

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

不用q的层遍历,算个特例吧

[复杂度]:Time complexity: O( 并没有新建一棵树) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

不用q的层遍历

[算法思想:递归/分治/贪心]:递归

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

/**
* Definition for binary tree with next pointer.
* public class TreeLinkNode {
* int val;
* TreeLinkNode left, right, next;
* TreeLinkNode(int x) { val = x; }
* }
*/
public class Solution {
public void connect(TreeLinkNode root) {
//ini: curr, head, prev
TreeLinkNode head = root;
TreeLinkNode curr = null;
TreeLinkNode prev = null; while (head != null) {
//ini
curr = head;
head = null;
prev = null; while (curr != null) {
if (curr.left != null) {
if (prev != null)
//join up
prev.next = curr.left;
else head = curr.left;
//update prev
prev = curr.left;
} if (curr.right != null) {
if (prev != null)
//join up
prev.next = curr.right;
else head = curr.right;
//update prev
prev = curr.right;
}
//in the same level
curr = curr.next;
} }
}
}

117. Populating Next Right Pointers in Each Node II 计算右边的附属节点的更多相关文章

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

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

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

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

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

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

  6. 117. Populating Next Right Pointers in Each Node II

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

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

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

  8. 117. Populating Next Right Pointers in Each Node II (Tree; WFS)

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

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

随机推荐

  1. 【yii2从Apache迁移到nginx上访问报500错误】

    [yii2从Apache迁移到nginx上访问报500错误] 今天迁移yii2项目从Apache到nginx,出现了几个小问题,记录一下 index.php 加上 error_reporting(E_ ...

  2. 基于Oracle的EntityFramework的WEBAPI2的实现(一)——准备工作

    目前在.net的范围内,好的而且方便的ORM的真的不是很多,与VS集成方便的也就当属EntityFramework(以下简称EF,不知道为什么,总EF这个缩写好不专业).但是,好多公司使用的又是ORA ...

  3. 02 - Unit01:服务器返回数据的json处理+搭建项目环境

    服务器返回数据的json处理+搭建项目环境 服务器返回数据的json处理 springMVC JSP响应流程 请求 -->DispatcherServlet -->HandlerMappi ...

  4. MQTT 协议 Client ID 长度不能超过23个字符

    今天遇到一个MQTT的问题,MqttException: MQIsdp ClientId > 23 bytes ClientId的长度大于23时,无法链接MQTT服务器. 经过查看协议发现:客户 ...

  5. qt在动态库里面加载widget的例子

    testDll和testExe项目 备注:windows下dll内不需要new QApplication, linux和mac下面需要在动态库里面new QApplication testdll.h ...

  6. 转:mysql加锁处理分析

    MySQL/InnoDB的加锁分析,一直是一个比较困难的话题.我在工作过程中,经常会有同事咨询这方面的问题.同时,微博上也经常会收到MySQL锁相关的私信,让我帮助解决一些死锁的问题.本文,准备就My ...

  7. idea 破解代码

    下面是idea的破解方法,手动执行对应的main方法即可.亲测Idea 14 完美破解~~废话少说,上代码... package com.jd.serializable; import java.ma ...

  8. Java复习——枚举与注解

    枚举 枚举就是让某些变量的取值只能是若干固定值中的一个,否则编译器就会报错,枚举可以让编译器在编译阶段就控制程序的值,这一点是普通变量无法实现的.枚举是作为一种特殊的类存在的,使用的是enum关键字修 ...

  9. 数据结构和算法之:二分法demo

    package com.js.ai.modules.pointwall.testxfz; class OrdArray{ private long[] a; private int nElems; p ...

  10. OD 实验(十一) - 对一个程序的破解

    程序: 点击安装程序 这是一个拼图程序 点击 Options -> Flash Sizes 程序会提示是未注册版本 点击一些选项的时候会提示该程序只给注册的用户 点击 Register 随便输入 ...