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 ...
随机推荐
- [WinAPI] 串口读写
#include <stdio.h> #include <stdlib.h> #include <windows.h> HANDLE hComm; OVERLAPP ...
- CSS3与页面布局学习总结
目录 一.BFC与IFC 1.1.BFC与IFC概要 1.2.如何产生BFC 1.3.BFC的作用与特点 二.定位 2.2.relative 2.3.absolute 2.4.fixed 2.5.z- ...
- 【原创】jmeter3.0在beanshell中输入中文乱码以及字体大小的更改
我使用的是最新的jmeter3.0版本,新建一个beanshell sampler,在里面输入中文,发现显示的是乱码,而且字体非常小,看着吃力,调研了一下,可以在bin/jmeter.properti ...
- iOS开发-迭代器模式
迭代器模式(Iterator),提供一种方法顺序访问一个聚合对象中的各种元素,而又不暴露该对象的内部表示.开发过程中,我们可能需要针对不同的需求,可能需要以不同的方式来遍历整个整合对象,但是我们不希望 ...
- iOS开发----三目运算符
一.三目运算符 1.基本格式 : (关系表达式) ? 表达式1 : 表达式2; 执行流程 : 关系表达式为 真 返回表达式1 关系表达式为假 返回表达式2 2.写一个例子来看一下三目运算符的使用: ...
- JAVA开发工具eclipse中@author怎么改
1:JAVA开发工具eclipse中@author怎么改,开发的时候为了注明版权信息. 用eclipse开发工具默认的是系统用户,那么怎么修改呢 示例如图所示 首先打开Eclipse--->然后 ...
- android WebView控件显示网页
有时需要app里面显示网页,而不调用其他浏览器浏览网页,那么这时就需要WebView控件.这个控件也是很强大的,放大,缩小,前进,后退网页都可以. 1.部分方法 //支持javascriptweb.g ...
- 聊聊 App Store 的产品和推广运营攻略
在这个工匠的时代,越来越多开发者投入了手机应用的开发圈子.如何才能在激烈的竞争中脱颖而出,少走弯路呢?我们跟空中网负责iPhone游戏的运营和推广的洪亮进行了一次交流,得到了不少经验和诀窍,值得分享给 ...
- css之属性及剩余的选择符
今天的课程加速了,比平时快了些,但觉得很不错.nice~ 属性选择符 E[att] 选择具有att属性的E元素. input[type]{color: #red;} <input t ...
- iOS开发实用技巧—项目新特性页面的处理
iOS开发实用技巧篇—项目新特性页面的处理 说明:本文主要说明在项目开发中会涉及到的最最简单的新特性界面(实用UIScrollView展示多张图片的轮播)的处理. 代码示例: 新建一个专门的处理新特性 ...