LeetCode OJ 116. 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
Subscribe to see which companies asked this question
解答
利用已经连好的next指针进行广度优先遍历,注意是每次处理下一层,所以下一层为NULL的时候就不需要处理了。
/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* struct TreeLinkNode *left, *right, *next;
* };
*
*/
void connect(struct TreeLinkNode *root) {
struct TreeLinkNode *head, *next_head, *pNode, *pre_node;
if(NULL == root){
return;
}
root->next = NULL;
next_head = root;
){
head = next_head;
next_head = next_head->left;
if(NULL == next_head){
break;
}
for(pNode = head; pNode != NULL; pNode = pNode->next){
if(pNode != head){
pre_node->next = pNode->left;
}
pNode->left->next = pNode->right;
pre_node = pNode->right;
}
pre_node->next = NULL;
}
}
LeetCode OJ 116. Populating Next Right Pointers in Each Node的更多相关文章
- 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 OJ】Populating Next Right Pointers in Each Node II
Problem Link: http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ OK... ...
- LeetCode OJ:Populating Next Right Pointers in Each Node II(指出每一个节点的下一个右侧节点II)
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- 【LeetCode OJ】Populating Next Right Pointers in Each Node
Problem Link: http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/ Just trav ...
- 【一天一道LeetCode】#116. Populating Next Right Pointers in Each Node
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
- 【LeetCode】116. Populating Next Right Pointers in Each Node
题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode ...
- LeetCode OJ 117. Populating Next Right Pointers in Each Node II
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode OJ:Populating Next Right Pointers in Each Node(指出每一个节点的下一个右侧节点)
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- 【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
随机推荐
- linux 网络编程比较好的文章
讲解epoll的, 算是比较详细的. 直接看使用部分就行. http://blog.csdn.net/xiajun07061225/article/details/9250579
- mysql数据库常规命令操作
1.MySQL数据库导出命令 1.导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 导出的文件名 mysqldump -u wcnc -p smgp_apps_wcn ...
- 转载-------makefile 使用总结
转载自:http://www.cnblogs.com/wang_yb/p/3990952.html 1. Makefile 简介 Makefile 是和 make 命令一起配合使用的. 很多大型项目的 ...
- jquery ajax 返回值 中文时乱码或变成问号解决方法
转载自jquery的 ajax返回值为中文时乱码解决方法 用jquery的ajax,遇到个问题,服务器端从数据库取到的数据没有出现中文乱码问题(日志打出来是没有乱码的),但是异步传到客户的时候却出现了 ...
- iOS开发 GET、POST请求方法(NSURLSession篇)
NSURLConnection,在iOS9被宣布弃用,本文不使用NSURLConnection进行网络编程,有兴趣的童鞋可以参考: [iOS开发 GET.POST请求方法(NSURLConnectio ...
- 使用 jquery 获取当前时间的方法
我们在写一些效果时,经常要用到 jquery 获取当前时间,但是jquery 目前并没有提供直接获取当前时间的 api 或者函数,所以我们还是得用原生的 javascript 时间对象 Date 来获 ...
- javascript 闭包最简单理解
首先说3点与闭包有关系的东西. 一.变量的作用域 变量的作用域不难理解. 1.函数内部可以访问函数外部的变量,而函数外部不能访问函数内部的变量. 2.如果在函数内定义变量的时候,不加var,那么是全局 ...
- iOS富文本
背景:前些天突然想做一个笔记本功能,一开始,觉得挺简单的呀,一个UITextView,网络缓存也不干了,直接本地NSUserDefault存储,然后完事了,美工,弄几张好看的图片,加几个动画,也就这样 ...
- [转] ACM中国国家集训队论文集目录(1999-2009)
国家集训队1999论文集 陈宏:<数据结构的选择与算法效率——从IOI98试题PICTURE谈起>来煜坤:<把握本质,灵活运用——动态规划的深入探讨>齐鑫:<搜索方法中的 ...
- js一个抽奖的例子
朋友公司开年会,帮忙写了个抽奖的demo,源码如下,github中有程序: html: <header> lottery demo </header> <div clas ...