LeetCode: Populating Next Right Pointers in Each Node 解题报告
Populating Next Right Pointers in Each Node Total
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
我们可以用递归处理左右子树. 这个算法可能会消耗O(h)的栈空间。
/* 试一下 recursion */
public void connect(TreeLinkNode root) {
if (root == null) {
return;
} rec(root);
} public void rec(TreeLinkNode root) {
if (root == null) {
return;
} if (root.left != null) {
root.left.next = root.right;
} if (root.right != null) {
root.right.next = root.next.left;
} rec(root.left);
rec(root.right);
}
2014.1229 redo:
/*
3. A recursion version.
*/
public void connect3(TreeLinkNode root) {
if (root == null || root.left == null) {
return;
} root.left.next = root.right;
root.right.next = root.next == null ? null: root.next.left; connect(root.left);
connect(root.right);
}
SOLUTION 2
用层次遍历也可以相当容易解出来,而且这种方法用在下一题一点不用变。
但是 这个解法不能符合题意。题目要求我们使用 constant extra space.
/*
* 使用level traversal来做。
* */
public void connect1(TreeLinkNode root) {
if (root == null) {
return;
} TreeLinkNode dummy = new TreeLinkNode(0);
Queue<TreeLinkNode> q = new LinkedList<TreeLinkNode>();
q.offer(root);
q.offer(dummy); while (!q.isEmpty()) {
TreeLinkNode cur = q.poll();
if (cur == dummy) {
if (!q.isEmpty()) {
q.offer(dummy);
}
continue;
} if (q.peek() == dummy) {
cur.next = null;
} else {
cur.next = q.peek();
} if (cur.left != null) {
q.offer(cur.left);
} if (cur.right != null) {
q.offer(cur.right);
}
}
}
2014.1229 redo:
1.
/*
1. Iterator.
*/
public void connect1(TreeLinkNode root) {
if (root == null) {
return;
} Queue<TreeLinkNode> q = new LinkedList<TreeLinkNode>();
q.offer(root); while (!q.isEmpty()) {
int size = q.size(); for (int i = 0; i < size; i++) {
TreeLinkNode cur = q.poll(); // ERROR 2: forget to determine if root don't have left and right.
if (cur.left == null) {
return;
} cur.left.next = cur.right;
cur.right.next = cur.next == null ? null : cur.next.left;
// bug 1: should put the offer inside the for loop
q.offer(cur.left);
q.offer(cur.right);
}
}
}
2.
/*
2. Iterator. More simple version.
*/
public void connect2(TreeLinkNode root) {
if (root == null) {
return;
} Queue<TreeLinkNode> q = new LinkedList<TreeLinkNode>();
q.offer(root); while (!q.isEmpty()) {
int size = q.size(); for (int i = 0; i < size; i++) {
TreeLinkNode cur = q.poll(); // bug 1: should judge the size!
cur.next = (i == size - 1) ? null: q.peek(); if (cur.left != null) {
q.offer(cur.left);
q.offer(cur.right);
}
}
}
}
SOLUTION 3
把层次遍历修改一下,就是下面的解法了,我们使用2个循环,一个指针P1专门记录每一层的最左边节点,另一个指针P2扫描本层,把下一层的链接上。
下层链接完成后,将P1移动到它的左孩子即可。
这个算法的空间复杂度是O(1). 没有额外的空间。
/*
The version that only has O(1) space complexity.
*/
public void connect(TreeLinkNode root) {
if (root == null) {
return;
} Iterator(root);
} public void Iterator(TreeLinkNode root) {
if (root == null) {
return;
} TreeLinkNode leftEnd = root; while(leftEnd != null) {
// go through the current level and link the next level.
TreeLinkNode cur = leftEnd;
while (cur != null) {
if (cur.left == null) {
break;
} cur.left.next = cur.right;
// 一定要记得判null.
cur.right.next = cur.next == null ? null: cur.next.left; cur = cur.next;
} // get to the next level.
leftEnd = leftEnd.left;
}
}
2014.1229 redo:
/*
4. Another constant iterator version.
*/
public void connect(TreeLinkNode root) {
if (root == null) {
return;
} TreeLinkNode leftEnd = root;
while (leftEnd != null && leftEnd.left != null) {
TreeLinkNode cur = leftEnd;
while (cur != null) {
cur.left.next = cur.right;
cur.right.next = cur.next == null ? null: cur.next.left; cur = cur.next;
} leftEnd = leftEnd.left;
}
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/Connect_2014_1229.java
LeetCode: Populating Next Right Pointers in Each Node 解题报告的更多相关文章
- 【LeetCode】116. 填充每个节点的下一个右侧节点指针 Populating Next Right Pointers in Each Node 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- LeetCode:Populating Next Right Pointers in Each Node I II
LeetCode:Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeL ...
- [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 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 II @ Python
原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题意: Follow up ...
- LeetCode: Populating Next Right Pointers in Each Node II 解题报告
Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...
- 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 ...
随机推荐
- libevent个人理解
1.利用了前置声明来在c语言的基础上进行封装操作.即在include目录下防止event.h等头文件,在这些头文件中只暴露struct的声明却不暴露其定义,对于如event_base等结构的操作均使用 ...
- ZOJ2750_Idiomatic Phrases Game(最短路)
Idiomatic Phrases Game Time Limit: 2 Seconds Memory Limit: 65536 KB Tom is playing a game calle ...
- Unix 网络编程 读书笔记2
第三章 套接字编程简介 每一个 Socket 都用一个半相关描述:{协议,本地地址,本地端口}一个完整的 Socket 则用一个相关描述{协议,本地地址,本地端口,远程地址,远程端口}每一个 Sock ...
- Linux 添加硬盘设备
fdisk命令用于管理磁盘分区,格式为:“fdisk [磁盘名称]”. 管理Linux系统中的硬盘设备最常用的方法就当属是用fdisk命令了,这条命令提供了添加.删除.转换分区等等功能于一身的“一站式 ...
- Fiddler 中请求时间的显示列
在Tool bar上面找到 Rules->CustomRules 在class Handlers{ 里面添加 //----------------------------显示请求时间,显示毫 ...
- Oracle EBS WMS功能介绍(二)
Oracle EBS WMS功能介绍(二) (版权声明,本人原创或者翻译的文章如需转载,如转载用于个人学习,请注明出处.否则请与本人联系,违者必究) 出货物流逻辑主要包括 1. 打包.能够进 ...
- 判断是否为SIM卡联系人
判断是否为SIM卡联系人 在AsyncQueryContacts类中. private List<TxrjAccount> accounts = new ArrayList<Txrj ...
- 原创+转发:微信小程序navigator、redirectTo、switchTab几种页面跳转方式
什么是事件? 事件是视图层到逻辑层的通讯方式. 事件可以将用户的行为反馈到逻辑层进行处理. 详解(以常见的tap点击事情为例) 模板.wxml代码: <view id="tapTest ...
- 设置sqlplus访问远程oracle数据库的最快方法
设置sqlplus访问远程oracle数据库的最快方法 时间:2010-01-21 10:57来源: 作者: 点击: 2次 设置sqlplus访问远程oracle数据库的最快方法,如果要连接远程数据库 ...
- 利用HttpWebRequest模拟表单提交 JQuery 的一个轻量级 Guid 字符串拓展插件. 轻量级Config文件AppSettings节点编辑帮助类
利用HttpWebRequest模拟表单提交 1 using System; 2 using System.Collections.Specialized; 3 using System.IO; ...