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 ...
随机推荐
- Raspberry pi(-) Mac下安装系统
购买树莓派一般的操作说明书都是windows下的,因为本人只有MBP,外加虚拟机Parallels Desktop无法识别SD卡,所以就直接参考Mac教程了. 1.系统镜像官网下载 推荐用Raspbi ...
- Spring 之注解事务 @Transactional
众所周知的ACID属性: 原子性(atomicity).一致性(consistency).隔离性(isolation)以及持久性(durability).我们无法控制一致性.原子性以及持久性,但可以 ...
- The PHP Package 之 monolog[转]
Monolog 发送你的日志到文件.到sockets.到邮箱.到数据库或(和)者其他网路存储服务(云).Monolog可以做到同时保存到一个或多个存储介质(后面的栈冒泡处理). 安装 $ comp ...
- bond汇总
七种bond模式说明: 第一种模式:mod=0 ,即:(balance-rr) Round-robin policy(平衡抡循环策略)特点:传输数据包顺序是依次传输(即:第1个包走eth0,下一个包就 ...
- cocoapods安装及常用命令
如果不是因为我重装了电脑,我大概也不会写这篇博客,网上关于cocoapods的安装可谓是不可胜数. 但是对于新版的系统来说,的确存在很多的坑点,这点不是我一个人遇到的问题 如果是新装的系统,默认是安装 ...
- C语言PIC16 serial bootloader和C#语言bootloader PC端串口通信程序
了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 新PIC16 Boot ...
- re正则表达式
1.matchimport rem = re.match("[0-9]{0,10}","7564565abcdefg") #match从开头开始匹配if m: ...
- spring代理模式 service远程调用,插件执行
最近,研究了一下平台远程调用的过程,和service层插件执行的原理,记录一下. 1.远程service调用过程 首先看一下类的继承结构 封装调用处理过程 封装service调用接口 封装servic ...
- SQL 字符串处理大全
select语句中只能使用sql函数对字段进行操作(链接sql server),select 字段1 from 表1 where 字段1.IndexOf("云")=1;这条语句不对 ...
- Host基本概念
TSO TSO是Time Sharing Option的缩写,是MVS的命令输入处理器. TSO是在操作系统的管理下,用来支持ISPF菜单式会话系统和资源管理设备( ...