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 *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
乍一看很难,理清思路后很简单的。
/**
* 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||root->left==NULL)
return;
root->left->next=root->right;
if(root->next!=NULL)
root->right->next=root->next->left;
connect(root->left);
connect(root->right);
return ;
}
};
II
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
/**
* 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(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 ——II仍然需要认真看看的更多相关文章
- Populating Next Right Pointers in Each Node(I and II)
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- 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 * ...
- 【leetcode】Populating Next Right Pointers in Each Node I & II(middle)
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
随机推荐
- 使用PowerDesigner建模
一.使用PowerDesigner建模 在数据库概念设计中已经分析了本系统中主要的数据应实体对象,通过这些实体可以得出数据表结构的基本模型,最终实施到数据库中, 形成完整的数据结构.本系统将使用Pow ...
- Picture POJ - 1177 (线段树-扫描线)
A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wa ...
- POJ 2112 二分+最大流
Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 17297 Accepted: 6203 ...
- mac之os x系统下搭建nodejs+express4.x+mongodb+gruntjs整套前端工程
第一次在Mac OS X上搭建前端开发环境,做一个小小记录,包括一些与windows系统的区别和常用快捷键 首先,在进行环境搭建之前先来看一下苹果系统的“cmd”,也就是Terminal(终端). 打 ...
- Scrapy中的Callback如何传递多个参数
在scrapy提交一个链接请求是用 Request(url,callback=func) 这种形式的,而parse只有一个response参数,如果自定义一个有多参数的parse可以考虑用下面的方法实 ...
- bzoj 2038 莫队入门
http://www.lydsy.com/JudgeOnline/problem.php?id=2038 题意:多次询问区间内取出两个相同颜色的种类数 思路:由于不是在线更新,那么可以进行离线查询,而 ...
- elementui input样式覆盖 头部小图等
.nav-right >>> .keywords .el-input__inner { -webkit-appearance: none; background-color: #F3 ...
- Angular2.0 基础: 环境搭建
最近在学习Angular2的使用,其实看过Angular2 文档的都知道,相比于之前的Angular1,Angular2 的改动还是挺大的. 而对于‘angular2 的本地开发环境的搭建的中,我们首 ...
- Java线程总结(一)
首先,先贴上一个简单的线程实例: public class MyThread extends Thread{ @Override public void run(){ try { for (int i ...
- mave的依赖范围
compile(编译范围) compile是默认的范围:如果没有提供一个范围,那该依赖的范围就是编译范 围.编译范围依赖在所有的classpath中可用,同时它们也会被打包. provided(已提供 ...