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 ...
随机推荐
- Maven使用中的常见问题整理
1.更新eclipse的classpath加入新依赖 1.在dependencyManagement里面加入包括版本在内的依赖信息,如: <dependency> <groupId ...
- 性能优化之mysql索引优化
sql及索引优化 如何通过慢查询日志发现有问题的sql? 查询次数多且每次查询占用时间长的sql通常为pt-query-digest分析的前几个查询 IO大的sql注意pt-query-digest分 ...
- erlang的一些小技巧(不定期更新)
在任意节点热更新代码 rpc:call(Node,c,l,[Mod]) c和l的指的是code,library Erlang Shell隐藏的小技巧 f(). %%把所有绑定变量释放掉 f(Val). ...
- JVM内存管理之GC算法精解(五分钟让你彻底明白标记/清除算法)
相信不少猿友看到标题就认为LZ是标题党了,不过既然您已经被LZ忽悠进来了,那就好好的享受一顿算法大餐吧.不过LZ丑话说前面哦,这篇文章应该能让各位彻底理解标记/清除算法,不过倘若各位猿友不能在五分钟内 ...
- FC Switch sfpshow
sfpshow - fault-finding on Brocade Fibre Channel Switches So you've hit a situation where a Fibre Ch ...
- 工作JS总结
获取 <inpout type="checkbox" value="1" /> 多选项的value /*获取checkbox的全部选中项 使用方法: ...
- Java 面向对象 初探
public class test { public static void main(String[] args) { // 利用new关键字创建了一个Person对象 Person p = new ...
- 第三章:Hadoop简介及配置Hadoop-1.2.1,hbase-0.94.13集群
前面给大家讲了怎么安装Hadoop,肯定会有人还是很迷茫,装完以后原来就是这个样子,但是怎么用,下面,先给大家讲下Hadoop简介:大致理解下就OK了 hadoop是一个平台,提供了庞大的存储和并行计 ...
- poj-2828 Buy Tickets(经典线段树)
/* Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 10207 Accepted: 4919 Descr ...
- Entity Framework使用汇总
EF的发展历程 还是先来说一下EF从诞生到现在这几年的发展历程吧.在EF最初的版本中,作为一个ORM组件其通过EDM文件(里面是一些xml)来配置数据库与实体类之间的映射,实现数据进出数据库的控制.最 ...