[抄题]:

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. tar 打包处理文件

    基本格式:tar [Options] file_archive //注意tar的第一参数必须为命令选项,即不能直接接待处理文件 Option是由三部分组成,分别是操作类型(创建,查看,解压),压缩处理 ...

  2. bzoj1089严格n元树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1089 这是一种套路:记录“深度为 i ”的话,转移需要讨论许多情况:所以可以记录成“深度&l ...

  3. RK3288 增加双屏异显 eDP+LVDS

    CPU:RK3288 系统:Android 5.1 下面是官方文档中的信息. 1.rk3288 支持的显示接口可以任意组合. 2.双屏异显时,一个显示接口当主屏,另一个当副屏:主副屏由板级 dts 文 ...

  4. NFS搭建与配置

    NFS应用场景是:A,B,C三台机器上需要保证被访问到的文件是一样的,A共享数据出来,B和C分别去挂载A共享的数据目录,从而B和C访问到的数据和A上的一致性 172.131.1.135  服务器端 1 ...

  5. centos7下安装docker 17.x

    docker的17.X版本与以前的docker安装有些不同,参考了下这篇文章http://www.itmuch.com/docker/docker-2/,以下是我的docker 17.X版本安装过程, ...

  6. 快速排序算法-python实现

    #-*- coding: UTF-8 -*- import numpy as np def Partition(a, i, j): x = a[i] #将数组的第一个元素作为初始基准位置 p = i ...

  7. ubuntu 进入单用户模式

    进入单用户模式:  按shift进入 1.开机到grub时,用上下键移到第二行的恢复模式,按e(注意不是回车) 即Ubuntu,With Linux 3.2.0-23-generic(recovery ...

  8. C语言函数返回值和变量类型

    前言 最近在刷题,在写矩阵的快速幂的题时,对于返回值是数组的程序,写的十分冗杂.借此机会,重新梳理下C语言中函数的返回值与变量类型的关系. 按照变量的寿命,可以分为三种类型 1.静态变量 寿命从程序开 ...

  9. Java运算符,关系运算符

    关系运算符介绍 下表为Java支持的关系运算符 表格中的实例整数变量A的值为10,变量B的值为20: 运算符 描述 例子 == 检查如果两个操作数的值是否相等,如果相等则条件为真. (A == B)为 ...

  10. java,js 解unicode

    import java.util.regex.Matcher; import java.util.regex.Pattern; public class UnicodeDecodeDataConver ...