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-right-pointers-in-each-node/
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) {
TreeLinkNode *p = root;
while(p && p->left) {
p->left->next = p->right;
TreeLinkNode *pre = p, *cur = p->next;
while(cur) {
pre->right->next = cur->left;
cur->left->next = cur->right;
pre = cur;
cur = cur->next;
}
p = p->left;
}
}
};
Populating Next Right Pointers in Each Node II
OJ:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-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) {
TreeLinkNode *curListNode, *startNode, *curNode;
startNode = root;
while(startNode) {
curNode = startNode;
startNode = curListNode = NULL;
while(curNode) {
if(curNode->left) {
if(startNode == NULL) startNode = curNode->left;
if(curListNode == NULL) curListNode = curNode->left;
else { curListNode->next = curNode->left; curListNode = curListNode->next; }
}
if(curNode->right) {
if(startNode == NULL) startNode =curNode->right;
if(curListNode == NULL) curListNode = curNode->right;
else { curListNode->next = curNode->right; curListNode = curListNode->next; }
}
curNode = curNode->next;
}
}
}
};
29. Populating Next Right Pointers in Each Node && Populating Next Right Pointers in Each Node II的更多相关文章
- 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 ...
- Node.app – 用于 iOS App 开发的 Node.js 解释器
Node.app 是用于 iOS 开发的 Node.js 解释器,它允许最大的代码重用和快速创新,占用资源很少,为您的移动应用程序提供 Node.js 兼容的 JavaScript API.你的客户甚 ...
- Node.js实战项目学习系列(5) node基础模块 path
前言 前面已经学习了很多跟Node相关的知识,譬如开发环境.CommonJs,那么从现在开始要正式学习node的基本模块了,开始node编程之旅了. path path 模块提供用于处理文件路径和目录 ...
- [Node.js] 00 - Where do we put Node.js
Ref: 前后端分离的思考与实践(五篇软文) 其实就是在吹淘宝自己的Midway-ModelProxy架构. 第一篇 起因 为了提升开发效率,前后端分离的需求越来越被重视, 同一份数据接口,我们可以定 ...
- Node.js 学习笔记(一)--------- Node.js的认识和Linux部署
Node.js 一.Node.js 简介 简单的说 Node.js 就是运行在服务端的可以解析并运行 JavaScript 脚本的软件. Node.js 是一个基于Chrome JavaScript ...
- Node“getTextContent() is undefined for the type Node”处理办法
最近一个项目在MyEclipse导入后总是报getTextContent() is undefined for the type Node错误. 经过查找原来是因为Node类为JDK中自带的(org. ...
- nohup /usr/local/node/bin/node /www/im/chat.js >> /usr/local/node/output.log 2>&1 &
nohup和&后台运行,进程查看及终止 &后台运行 登出ssh终端,进程会被自动kill掉 但是nohup >>XX.log 2>&1 & 登出终 ...
- node基础09:第2个node web服务器
1.同时输出文字与图片 在前几个小课程中,我会学会了 从服务器中读取文字字符,并且向浏览器中输出 从服务器中读取图片文件,并且向浏览器中输出 这节课中,我学会了同时向浏览器输出文字,图片.对此,我感到 ...
- [Node.js] Using npm link to use node modules that are "in progress"
It is some times convenient, even necessary, to make use of a module that you are working on before ...
随机推荐
- enmo_day_07
数据备份 物理备份 : 底层数据块 逻辑备份 :exp(export), imp(import) 导入导出工具,提取成dump文件,再将dump文件放入数据库 expdp, impdp 数据蹦 uti ...
- Http协议访问DataSnap Rest 服务器
用TIDHttp访问DataSnap Rest服务器,在服务器采用了用户验证的情况下,客户端需要注意下面的细节,否则不能正常连接. 假如服务器有如下的用户验证: procedure TSC.DSAut ...
- In close() at SocketHttpClientConnection in Android
In close() at SocketHttpClientConnection Error In Android. when i tried to acess network data on Mai ...
- SharePoint 2016 开发 工具Preview发布
博客地址:http://blog.csdn.net/FoxDave 之前装了SharePoint,但是并不能在Visual Studio 2015里面做开发,因为没有相应的office tool. 但 ...
- mysql linux终端登陆
mysql -uroot -hlocalhost -psorry 设置远程登录 用户名及密码 GRANT ALL PRIVILEGES ON *.* TO root@"%" IDE ...
- (转)Javascript匿名函数的写法、传参、递归
(原)http://www.veryhuo.com/a/view/37529.html (转)javascript匿名函数的写法.传参和递归 javascript匿名函数的写法.传参和递归 http: ...
- iOS-音频和视频
一.视频 视频播放器需要添加MediaPlayer.framework. 视频播放主要提供了两个类,一个MPMoviePlayerController, 另一个是MPMoviePlayerViewCo ...
- redis-persist上线
九月份惨不忍睹,因为代码质量不够高,直接被Boss喷成了筛子.被反复教育说要高质量的代码,要可维护.高性能…… 幸而,最后一周终于在紧张的加班中,灰度上线redis-land-go了,项目也改名为re ...
- asp.net 前台通过Eval()绑定动态显示样式
1.a标签链接 <%#Eval("ConfigCode").ToString().ToLower() == "publishtext" ? "& ...
- 【转载】AngularJs 指令directive之controller,link,compile
关于自定义指令的命名,你可以随便怎么起名字都行,官方是推荐用[命名空间-指令名称]这样的方式,像ng-controller.不过你可千万不要用 ng-前缀了,防止与系统自带的指令重名.另外一个需知道的 ...