[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 ...
随机推荐
- Determining IP information for eth0...failed 错误解决
问题描述:虚拟机使用wget命令上网,执行service network restart后出现如下错误Determining IP information for eth0...failed解决办法: ...
- 存储设备的DDP功能详解
http://blog.csdn.net/u013394982/article/details/18259015 DDP功能,即Dynamic Disk Pool,它是除了现有的RAID0,1,10, ...
- BOM和DOM(cs)
前戏 到目前为止,我们已经学过了JavaScript的一些简单的语法.但是这些简单的语法,并没有和浏览器有任何交互. 也就是我们还不能制作一些我们经常看到的网页的一些交互,我们需要继续学习BOM和DO ...
- centos7防火墙使用方法
参考网站:https://blog.csdn.net/achang21/article/details/52538049 添加开放指定端口: [root@yao bin]# firewall-cmd ...
- mysql 5.7.10使用dbforget Studio 连接异常
提示:The 'INFORMATION_SCHEMA.SESSION_VARIABLES' feature is disabled; see the documentation for 'show_c ...
- UI5-文档-4.19-Reuse Dialogs
在此步骤中,我们将扩展重用概念,并在组件级别调用对话框. 在步骤16中,我们创建了一个对话框作为片段,以使其可跨视图或跨整个应用程序重用.但是我们将检索对话框实例和分别打开和关闭对话框实例的逻辑放置在 ...
- vue常用
vue常用的路由的状态管理
- JVM内存管理基础
JVM 虚拟机架构(图片来源: 浅析Java虚拟机结构与机制) JVM 内存区域 JVM会将Java进程所管理的内存划分为若干不同的数据区域. 这些区域有各自的用途.创建/销毁时间: (图片来源: ...
- Supervisor 进程管理工具
简介: Supervisor 进程管理工具 一.安装 shell > yum -y install python-pip shell > pip install supervisor # ...
- Haskell语言学习笔记(68)HDBC
安装 HDBC-Sqlite3 $ cabal install HDBC-Sqlite3 Installed HDBC-sqlite3-2.3.3.1 Prelude> :m Database. ...