[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 ...
随机推荐
- 网络性能测试工具iperf详细使用图文教程(转)
Iperf是一个网络性能测试工具.Iperf可以测试TCP和UDP带宽质量.Iperf可以测量最大TCP带宽,具有多种参数和UDP特性.Iperf可以报告带宽,延迟抖动和数据包丢失.利用Iperf这一 ...
- sklearn的GridSearchCV例子
class sklearn.model_selection.GridSearchCV(estimator, param_grid, scoring=None, fit_params=None, n_j ...
- [转]Tornado get/post请求异步处理框架分析
转自: http://m.blog.csdn.net/blog/joeyon/41956027 首先说下环境,python2.7,Tornado3.0 然后本文讨论的不是Tornado底层如何用epo ...
- Web 数据源
问题描述: ClassCastException:类型转换异常 问题代码: private static LinkedList<Connection> pool = (LinkedList ...
- GameObject数组
一.声明 GameObject[] rock = ]; 二.遍历 可以先声明一个GameObject的数组GameObject[], 然后把需要遍历的对象放进去,然后可以用下面两种方式遍历:1.for ...
- 用docker搭建php+nginx+laravel的开发环境
制作镜像 由于官方php:7.2.2-fpm-alpine3.7 镜像不含composer,而单独做一个composer镜像又会依赖php镜像,所以应该在php镜像中添加composer.Docker ...
- php 数组随机取值
array_rand()在你想从数组中取出一个或多个随机的单元时相当有用.它接受 input 作为输入数组和一个可选的参数 num_req,指明了你想取出多少个单元 - 如果没有指定,默认为 1. a ...
- Python正则总结
re总结 限定符(就是控制重复次数) ? 匹配前面的子表达式零次或一次.例如,"do(es)?" 可以匹配 "do" . "does" 中的 ...
- 3. HashMap和JSONObject用法
<%@page import="net.sf.json.JSONObject"%><%@page import="java.util.List" ...
- Redis hash数据结构
1, 新增一个 hash 或者 新增数据 => hset key field value 2, 获取某个字段值 => hset key field 3, 获取所有字段值 => hge ...