[leetcode]117. Populating Next Right Pointers in Each NodeII用next填充同层相邻节点
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
题意:
给定一个任意二叉树,其中每个节点都有next指针。
设法将next指针指向同一层右侧相邻节点。
思路:
递归,解法很巧妙。
以 1 为curr,用pre来连接curr.left:2 和 curr.right:3。因为此时根节点 1的next指向null, 退出for循环
1 ( curr )--->null
/ \
dummy(-1):pre--> 2---> 3
/ \ \
4 5 7 递归调用connect(dummy.next),因为dummy.next指向2,此时 2 为curr。用pre来连接curr.left:4 和 curr.right:5。
1
/ \
2(curr)---> 3
/ \ \
dummy(-1):pre--> 4--->5 7
继续走for循环, curr = curr.next, 此时curr为3。 继续用pre连接 curr.left(3的左子树为Null) 和 curr.right:7。
1
/ \
2 ---> 3(curr)
/ \ \
dummy(-1):pre--> 4--->5 --->7
因为此时curr的next指向null, 退出for循环。
继续递归调用connect(dummy.next)... 直至 root == null, return
代码:
public class Solution {
public void connect(TreeLinkNode root) {
if (root == null) return;
TreeLinkNode dummy = new TreeLinkNode(-1);
for (TreeLinkNode curr = root, prev = dummy;
curr != null; curr = curr.next) {
if (curr.left != null){
prev.next = curr.left;
prev = prev.next;
}
if (curr.right != null){
prev.next = curr.right;
prev = prev.next;
}
}
connect(dummy.next);
}
}
[leetcode]117. Populating Next Right Pointers in Each NodeII用next填充同层相邻节点的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- Leetcode#117 Populating Next Right Pointers in Each Node II
原题地址 二叉树的层次遍历. 对于每一层,依次把各节点连起来即可. 代码: void connect(TreeLinkNode *root) { if (!root) return; queue< ...
- 117. Populating Next Right Pointers in Each Node II 计算右边的附属节点
[抄题]: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNod ...
- 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@ [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 ...
随机推荐
- 联想Z510升级BCM94352HMB刷网卡白名单曲折经历
联想Z510笔记本:CPU I7 4702MQ没毛病 :内存4G DDR3不上虚拟机办公足够用: 硬盘升级为SSD240G足够用:有线网卡100M,真是垃圾,不过有线网卡是主板上的芯片,这个我可动不了 ...
- 字符串,hash
字符串1.有序的字符的集合,不可变2.s.swapcase() 大变小,小变大3.s.capitalize() 第一个大写4.s.casefold() 返回将字符串中所有大写字符转换为小写后生成的字符 ...
- java impl
java impl 是一个资源包,用来存放java文件的.在Java开发中,通常将后台分成几层,常见的是三层mvc:model.view.controller,模型视图控制层三层,而impl通常处于c ...
- CSS滚动条样式设置
webkit浏览器css设置滚动条 主要有下面7个属性 ::-webkit-scrollbar 滚动条整体部分,可以设置宽度啥的 ::-webkit-scrollbar-button 滚动条两端的按钮 ...
- UVA-755-排序
奇怪,我怎么还有一个排序题目没过 题意如下: 公司喜欢有难忘的电话号码,一个让电话号码变得难忘的方式是有一个拼读起来难忘的单词,比如,你可以呼叫University of Waterloo通过拨打难忘 ...
- python学习笔记一和PHP的一些对比
python和php一样是 解释性语言 php和php一样 定义变量不需要指定类型,C语言中的print函数 在python中也是适用的 python编码 适用缩进 4个空格,通常存储为UTF-8模 ...
- zabbix 主动模式监控
参考网站: http://www.mamicode.com/info-detail-1724685.html http://www.cnblogs.com/dadonggg/p/8609674.ht ...
- TDictionary 是delphi用的,c++builder用起来太吃力。
TDictionary 是delphi用的,c++builder用起来太吃力.c++还是用std::map代替.c++d map很好用啊.https://blog.csdn.net/ddkxddkx/ ...
- XE4 TStringDynArray 比 c6 的TStringList 好用 字符串 分解 分割 转换 TByteDynArray
TStringDynArray 动态数组 字符串 分解 分割 System::DynamicArray<System::UnicodeString> TByteDynArray, ...
- python限制函数执行时间
from:https://stackoverflow.com/questions/366682/how-to-limit-execution-time-of-a-function-call-in-py ...