117. Populating Next Right Pointers in Each Node II 计算右边的附属节点
[抄题]:
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分治法两个人走
[一句话思路]:
利用不写公式的递归,先单层遍历,再多层遍历
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 已经声明过,就开辟过空间,不需要再次声明。但是可以赋值。
- 同一层每个节点都要算,所以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 计算右边的附属节点的更多相关文章
- 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)
[LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 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 ...
- 【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 ...
- 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 ...
- 117. Populating Next Right Pointers in Each Node II
题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...
- 【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 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 ...
- 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 ...
随机推荐
- linux查找目录下的所有文件中是否含有某个字符串 (转)
查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri "IBM" 查找目录下的所有文件中是否含有某个字符串,并且只打印出文件名 find .|xar ...
- jQuery 性能优化技巧
原文地址:jQuery 性能优化技巧 博客地址:http://www.extlight.com 一.使用最新版本 jQuery 类库 二.合理使用选择器 # 推荐使用 $("#id" ...
- 通过Authentication Challenge来信任自签名Https证书
在开发阶段我们我们经常使用自签名的证书来部署我们的后台rest api.但是在iOS中调用的时候就会因为证书不被信任而调用api不成功.这时候我们就需要通过实现某些网络回调函数来自定义证书的验证逻辑. ...
- VS2003在vista/win7下搜索会出现僵死
1. VS2003在vista下搜索关键词的时候会出现僵死的问题的解决方案: VS2003快捷方式右击选中属性->兼容性页签 : 选中用兼容模式运行这个程序,下拉框中用windows xp2 ...
- 【转】使用 JMeter 完成常用的压力测试
本文介绍了 JMeter 相关的基本概念.并以 JMeter 为例,介绍了使用它来完成最常用的三种类型服务器,即 Web 服务器.数据库服务器和消息中间件,压力测试的方法.步骤以及注意事项. ...
- 模拟admin组件自己开发stark组件之自定义list_display,反向解析url
反向解析 在上一篇文章中,我们创建好了stark这个组件,一个应用一个表有四个默认的url,那么我们如何区别这些url,因为可能会有重复现象(本组件不会,因为前面拼接了应用名,表明,肯定唯一),概念请 ...
- SpringMVC+Spring+Mybatis -- 集成之旅
准备 首先介绍一下,我的工具使用的是STS, 需要的童鞋可以到官网下载:http://spring.io/tools/sts/all 使用STS是因为她集成了Maven进行 “包“ 管理以及自带 We ...
- Tkinter Anchors(锚)
Python GUI - Tkinter Anchors: 锚是用来定义文本的相对位置参考点 锚是用来定义文本的相对位置参考点. 这里是锚属性可以使用的常数列表. NW N NE W CENTER ...
- Python实践练习:多重剪贴板
题目 假定你有一个无聊的任务,要填充一个网页或软件中的许多表格,其中包含一些文本字段.剪贴板让你不必一次又一次输入同样的文本,但剪贴板上一次只有一个内容.如果你有几段不同的文本需要拷贝粘贴,就不得不一 ...
- 呕心沥血Android studio使用JNI实例
发现网上很多JNI的使用教程,也很详细,不过有的地方有些缺漏,导致很多小问题难以解决的,今天就来总结一下. 准备工作:下载NDK. 简单的说,要用到C/C++,就要用NDK.直接百度搜索然后去官网下载 ...