Populating Next Right Pointers in Each Node II--leetcode难题讲解系列
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
https://leetcode.com/problems/populating-next-right-pointers-in-each-node/
由于空间复杂度为O(1),广搜深搜不能使用,只能考虑递归迭代。充分利用parent的next指针,我们可以很容易的找到子节点的next指针。
// C++ RECURSIVE CODE:
class Solution {
public:
static void connect(TreeLinkNode* root){
if(root == NULL) return;
if(root->left) root->left->next = root->right;
if(root->next && root->right ) root->right->next = root->next->left;
connect(root->left);
connect(root->right);
}
};
实际上用栈也是会消耗空间的,迭代应该是最合适的办法。为了保持处理的一致性,我们可以给每一层加一个dummy头结点,然后根据parent层(利用next)决定child层的next。
// JAVA ITERATIVE CODE:
public class Solution {
public void connect(TreeLinkNode root) {
TreeLinkNode dummy = new TreeLinkNode(0);
while(root != null){
TreeLinkNode child = dummy;
dummy.next = null;
while(root != null){
if(root.left != null){
child.next = root.left;
child = child.next;
}
if(root.right != null){
child.next = root.right;
child = child.next;
}
root = root.next;
}
root = dummy.next;
}
}
}
class Solution:
# @param root, a tree node
# @return nothing
def connect(self, root):
dummychild = TreeLinkNode(0)
while root:
cur = dummychild
dummychild.next = None
while root:
if root.left:
cur.next = root.left
cur = cur.next
if root.right:
cur.next = root.right
cur = cur.next
root = root.next
root = dummychild.next
Follow up for problem "Populating Next Right Pointers in Each Node".
What if the given tree could be any binary tree? Would your previous solution still work?
Note:
- You may only use constant extra space.
For 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
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
唯一的区别是找子节点的next指针不是那么直接了,可以用函数根据不同情况来返回,其他的部分做法一样。
class Solution {
public:
TreeLinkNode* first(TreeLinkNode* root){
root = root->next;
while(root){
if(root->left) return root->left;
else if(root->right) return root->right;
root = root->next;
}
return NULL;
}
void connect(TreeLinkNode* cur){
TreeLinkNode* root = cur;
if(root == NULL) return;
while(root){
if(root->left){
root->left->next = root->right ? root->right: first(root);
}
if(root->right){
root->right->next = first(root);
}
root = root->next;
}
connect(cur->left);
connect(cur->right);
}
};
Populating Next Right Pointers in Each Node II--leetcode难题讲解系列的更多相关文章
- Populating Next Right Pointers in Each Node II leetcode java
题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...
- Populating Next Right Pointers in Each Node II [Leetcode]
Problem Description http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ ...
- 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】Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
- 29. Populating Next Right Pointers in Each Node && Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node OJ: https://oj.leetcode.com/problems/populating-next-rig ...
- Populating Next Right Pointers in Each Node,Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node Total Accepted: 72323 Total Submissions: 199207 Difficul ...
- 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 树 Populating Next Right Pointers in Each Node II
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...
- 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】117. Populating Next Right Pointers in Each Node II (2 solutions)
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
随机推荐
- crontab定时任务配置记录
一.前言 今天简单记录下crontab的配置 二.crontab目录 /etc/crontab 文件 这是系统运行的调度任务 /var/spool/cron 目录 用户自定义的crontab任务放在此 ...
- spring三种实例化bean的方式
1构造函数实例化 2静态工厂方法实例化 3实例工厂方法实例化 service接口: package service; public interface PersonService { public v ...
- 点击div外面该div消失(二)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Mongodb副本集
Replication:副本集 副本集可以将客户端的写操作分散到不同的服务器,可以用于灾难恢复,报告和备份. 副本集需要一个主服务器和一个备服务器,以及一个仲裁服务器.仲裁服务器决定将哪一个服务器作为 ...
- iOS中dyld缓存的实现原理是怎样的?
在iOS开发中,为了提升系统的安全性,很多系统库文件都被打包到一个缓存的文件当中即dyld缓存,那大家对dyld缓存了解多少呢?今天小编将和大家分享的就是一位iOS大神对dyld缓存的使用分析,一起来 ...
- C#程序中注释过多的8条理由
程序中中的注释,一般是有益处的,可以知晓程序的一些逻辑说明,或是参数解释.但是有些程序,因为注释太多,反而引起维护上的不方便,删掉了怕以后不能出现问题不好查找原因,不删除留在代码中,对程序的维护人员, ...
- CMSIS OS None
/* ---------------------------------------------------------------------- * Copyright (C) 2011 ARM L ...
- Radio Basics for RFID
Radio Basics for RFID The following is excerpted from Chapter 3: Radio Basics for UHF RFID from the ...
- OpenBTS的安装(转)
OpenBTS source code可以在这里下载:http://sourceforge.net/projects/openbts/ OpenBTS入门的各种问题可以在这里找到答案:http://g ...
- Apache Solr查询语法
常用: q - 查询字符串,必须的. fl - 指定返回那些字段内容,用逗号或空格分隔多个. start - 返回第一条记录在完整找到结果中的偏移位置,0开始,一般分页用. rows - 指定返回结果 ...