Populating Next Right Pointers in Each Node I, II——生成next树
1、
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
分析:
这道题之所以放上来是因为题目中的那句话:You may only use constant extra space
这就意味着,深搜是不能用的,因为递归是需要栈的,因此空间复杂度将是 O(logn)。毫无疑问广搜也不能用,因为队列也是占用空间的,空间占用还高于 O(logn)
难就难在这里,深搜和广搜都不能用,怎么完成树的遍历?
我拿到题目的第一反应便是:用广搜,接着发现广搜不能用,便犯了难。
看了一些提示,有招了:核心仍然是广搜,但是我们可以借用 next 指针,做到不需要队列就能完成广度搜索。
如果当前层所有结点的next 指针已经设置好了,那么据此,下一层所有结点的next指针 也可以依次被设置。
/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:
void connect(TreeLinkNode *root) {
if(root==NULL)
return; while(root->left){
TreeLinkNode *cur=root;
while(cur!=NULL){
cur->left->next=cur->right;
if(cur->next!=NULL){
cur->right->next=cur->next->left;
}
cur=cur->next;
}
root=root->left;
}
}
};
2、
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
随后题目做了一些更改:不一定是满二叉树。
解法的核心:递推思想 依然不需要改变,依然是依据当前层的next 指针,设置下一层的 next 指针。只是找结点麻烦些,我们定义了两个函数,findNextNodeNextLev用来找(n+1)层的下一个节点,findStartNodeNextLev 用来找下一层的起始节点。
class Solution {
public:
void connect(TreeLinkNode *root) {
if(NULL == root) return;
TreeLinkNode* start;
TreeLinkNode* curNode;
TreeLinkNode* nextNode;
while(root != NULL){
start = findStartNodeNextLev(root);
curNode = start;
nextNode = findNextNodeNextLev(root, start);
while(nextNode != NULL){
curNode -> next = nextNode;
curNode = nextNode;
nextNode = findNextNodeNextLev(root, curNode);
}
root = start;
}
}
private:
TreeLinkNode* findNextNodeNextLev(TreeLinkNode* &cur, TreeLinkNode* curNextLev){
if(cur -> left == curNextLev && cur -> right != NULL){
return cur -> right;
}else{
while(cur -> next != NULL){
cur = cur -> next;
if(cur -> left != NULL && cur -> left != curNextLev) return cur -> left;
if(cur -> right != NULL && cur -> right != curNextLev) return cur -> right;
}
}
return NULL;
} TreeLinkNode* findStartNodeNextLev(TreeLinkNode* node){
if(NULL == node) return NULL;
if(node -> left != NULL) return node -> left;
return findNextNodeNextLev(node, node -> left);
}
};
Populating Next Right Pointers in Each Node I, II——生成next树的更多相关文章
- 【leetcode】Populating Next Right Pointers in Each Node I & II(middle)
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- 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@ [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 ...
- Populating Next Right Pointers in Each Node I&&II ——II仍然需要认真看看
Populating Next Right Pointers in Each Node I Given a binary tree struct TreeLinkNode { TreeLinkNode ...
- [LeetCode 116 117] - 填充每一个节点的指向右边邻居的指针I & II (Populating Next Right Pointers in Each Node I & II)
问题 给出如下结构的二叉树: struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } ...
- LeetCode 题解:Populating Next Right Pointers in Each Node I & II 二有难度。考虑不全面。
每次应该把root同层的右侧节点传过来.如果没有,就传NULL. 同时,应该是先右后左. 感觉这次的代码还挺简洁的.. void construct(struct TreeLinkNode *root ...
- 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
题目链接: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 ...
随机推荐
- 听说你的模型损失是NaN
听说你的模型损失是NaN 有时候,模型跑着跑着,损失就莫名变NaN了.不过,经验告诉我们,大部分NaN主要是因为除数是0或者传给log的数值不大于0.下面说说是log出NaN的几种常见解决方法. 毕竟 ...
- 浅析 Node.js 的 vm 模块以及运行不信任代码
在一些系统中,我们希望给用户提供插入自定义逻辑的能力,除了 RPC 和 REST 之外,运行客户提供的代码也是比较常用的方法,好处是可以极大地减少在网络上的耗时.JavaScript 是一种非常流行而 ...
- linux下java命令行引用jar包
一般情况下: 如果java 文件和jar 包在同一目录 poi-3.0-alpha3-20061212.jar testTwo.java 编译: javac -cp poi-3.0-alpha3-2 ...
- c++中vector容器的功能及应用。
vector基本操作: 1.头文件 #include<vector>. 注:一定要加上using namespace std; 2.vector对象的创建: vector<int ...
- 建立RSA协商加密的安全信道
在基于TCP长连接的CS链路中,如何保证数据流的安全性是开发者最关注的问题之一.本文深入浅出的给大家介绍一下在TCP连接中,使用RSA协商加密的方式,建立一个安全加密的通信链路,保证数据传输的安全性. ...
- 【Luogu】P3396哈希冲突(根号算法)
题目链接 根号算法真的是博大精深啊……明明是暴力但复杂度就是能过 这也太强了吧!!! 预处理出p<=sqrt(n)的所有情况,耗时n根n 查询: 如果p<=根n,O1查表 如果p>= ...
- HDU-2448 Mining Station on the Sea
先根据不同的起点跑最短路,记录距离,从而建立二分图求最小匹配. 一开始我求最短路的时候我把港口直接加到图中,然后发现进了港口就不能出来了,所以连接港口的边就要从双向边改成单向边…………这也搞得我n和m ...
- 学习 WebService 第二步:知识准备——WSDL文件解析
原文地址:https://www.cnblogs.com/yzw23333/p/7245104.html Web service中一个 WSDL 对应一个 web service地址. 可以想象成一个 ...
- C# 图像旋转代码
方法一: public static Bitmap rotateImage(Bitmap b, float angle) { //create a new empty bitmap to hold r ...
- Unity中LoadLevel与LoadLevelAsync的区别
1.LoadLevel 同步加载 写法:Application.LoadLevel(“name”); 优点:读取场景使用同步的方法就可以,因为是同步方法所以读取的速度是最快的,也不用更新界面,因为同步 ...