Populating Next Right Pointers in Each Node 解答
Question
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.
- You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
For example,
Given the following perfect binary tree,
1
/ \
2 3
/ \ / \
4 5 6 7
After calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ / \
4->5->6->7 -> NULL
Solution 1 -- BFS
Key to the solution is to traverse tree level by level. Therefore, we can use BFS. Time complexity O(n), n is the number of nodes, and space cost is O(n).
/**
* 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) {
// We can use BFS to solve this problem
List<TreeLinkNode> current = new ArrayList<TreeLinkNode>();
List<TreeLinkNode> next;
if (root == null)
return;
current.add(root);
while (current.size() > 0) {
next = new ArrayList<TreeLinkNode>();
int length = current.size();
for (int i = 0; i < length; i++) {
TreeLinkNode currentTreeNode = current.get(i);
if (i < length - 1)
currentTreeNode.next = current.get(i + 1);
else
currentTreeNode.next = null;
if (currentTreeNode.left != null)
next.add(currentTreeNode.left);
if (currentTreeNode.right != null)
next.add(currentTreeNode.right);
}
current = next;
}
}
}
Solution 2 -- Recursive
Consider for one node, the connection is done when:
1. Its left node (if exists) connect to its right node.
2. Its right node (if exists) connect to its next node's left child.
3. Its left child and right child are all connected.
Note the prerequisite for this problem is perfect binary tree (every parent has two children). Time complexity O(n), space cost O(1).
/**
* 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) {
if (root == null)
return;
if (root.left != null)
root.left.next = root.right;
if (root.right != null && root.next != null)
root.right.next = root.next.left;
connect(root.left);
connect(root.right);
}
}
Solution 3 -- Four Pointers
We can use four pointers to traverse the tree.
(referrence: ProgramCreek)
/**
* 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) {
if (root == null)
return;
TreeLinkNode lastHead = root;//prevous level's head
TreeLinkNode lastCurrent = null;//previous level's pointer
TreeLinkNode currentHead = null;//current level's head
TreeLinkNode current = null;//current level's pointer while (lastHead != null) {
lastCurrent = lastHead;
currentHead = lastHead.left;
current = lastCurrent.left;
while (lastCurrent != null && current != null) {
current.next = lastCurrent.right;
current = current.next;
lastCurrent = lastCurrent.next;
if (lastCurrent != null) {
current.next = lastCurrent.left;
current = current.next;
}
}
lastHead = currentHead;
currentHead = null;
}
}
}
Populating Next Right Pointers in Each Node 解答的更多相关文章
- Populating Next Right Pointers in Each Node II 解答
Question Follow up for problem "Populating Next Right Pointers in Each Node". What if the ...
- 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 - Populating Next Right Pointers in Each Node
题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...
- [LeetCode] 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] Populating Next Right Pointers in Each Node 每个节点的右向指针
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- LEETCODE —— Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...
- LeetCode - Populating Next Right Pointers in Each Node II
题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...
- 【leetcode】Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
- 【leetcode】Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...
随机推荐
- obiz
ofbiz 安装 1. 由 binary 安装: 由 binary 安装非常简单, 以下是安装方法: 下载ofbiz-2.0-beta1-complete.tar.gz, 注意不是ofbiz-2.0- ...
- mook_百度百科
mook_百度百科 mook
- 用WIFI为什么连不上VPN
因为是做服务器的,经常需要通过VPN连接到公司的服务器处理一些事件. 但最近一次在寝室通过WIFI连接VPN时却报“错误 619:不能建立到远程计算机的连接,因用于此端口的连接已关闭”.我的第一反应是 ...
- 学习php常用算法
<?php /*学用php算法*/ /*1.冒泡法 *思路分析:在要排序的一组数中,对当前还未排好的序列, *从前往后对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒. *即,每 ...
- C#分层开发MySchool
分层开发之MYSCHOOL No.1实现登陆功能,验证用户名和密码.从数据库里进行匹配,看是否有符合要求的数据. 在DAL层编写代码,返回值为布尔类型.方法参数为(student实体类对象),使用参数 ...
- Kafka测试
准备工作 硬件:笔记本,windows10系统4核8G内存 软件:接口测试工具,以及kafka自带测试工具 影响测试结果配置分析 Borker num.network.thread=3 用于接收并处理 ...
- 多线程 AfxBeginThread 与 CreateThread 的区别
简言之: AfxBeginThread是MFC的全局函数,是对CreateThread的封装. CreateThread是Win32 API函数,前者最终要调到后者. 1>.具体说来,Cr ...
- 爆出错误:The Geometry has no Z values
ArcGis添加地图标注,爆出错误:The Geometry has no Z values 解决方法如下: public bool AddFeature( ESRI.ArcGIS.Geometry. ...
- ios 网络数据下载和JSON解析
ios 网络数据下载和JSON解析 简介 在本文中笔者将要给大家介绍ios中如何利用NSURLConnection从网络上下载数据,如何解析下载下来的JSON数据格式,以及如何显示数据和图片的异步下载 ...
- C# 操作系统防火墙
很多时候,我们的程序是通过网络通信(如TCP或者UDP协议+端口),而将制作好的程序安装包给客户用时,发现会出现不能通信的现象(或者在这台电脑是可以的,却在另一台不可以),原因是防火墙阻止了,需要添加 ...