LeetCode: Populating Next Right Pointer in Each Node
LeetCode: Populating Next Right Pointer 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
地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/
算法:用递归好简单有木有。按上面的例子,先完成左右子树的连接,然后,根的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) return ;
root->next = NULL;
connect(root->left);
connect(root->right);
TreeLinkNode *p = root->left;
TreeLinkNode *q = root->right;
while(p && q){
p->next = q;
p = p->right;
q = q->left;
}
}
};
第二题:
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
地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
算法:这道题与前面一题不同的是,这里的二叉树不再是完美二叉树,而是一颗普通的二叉树。由于是一颗普通的二叉树,所以沿着右指针走不一定会到达下一层的最后一个节点,因为这个右指针可能为空;同样,沿着左指针走也不一定会到达下一层的第一个节点,因为这个左指针可能为空。所以,我们需要一个找到当前节点下一层的第一个节点的函数,然后顺着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) return ;
root->next = NULL;
connect(root->left);
connect(root->right);
TreeLinkNode *p = root->left;
TreeLinkNode *q = root->right;
while(p && q){
TreeLinkNode *r = nextLevelFirst(p);
while(p->next){
p = p->next;
}
p->next = q;
p = r;
q = nextLevelFirst(q);
}
}
TreeLinkNode * nextLevelFirst(TreeLinkNode *p){
while(p){
if(p->left){
return p->left;
}else if(p->right){
return p->right;
}
p = p->next;
}
return NULL;
}
};
LeetCode: Populating Next Right Pointer in Each Node的更多相关文章
- [Leetcode] Populating next right pointer in each node ii 填充每个节点的右指针
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- [Leetcode] Populating next right pointer in each node 填充每个节点的右指针
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] 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 II 解题报告
Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...
- 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 tre ...
- [leetcode]Populating Next Right Pointers in Each Node II @ Python
原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题意: Follow up ...
随机推荐
- hdu1896 bjfu1268 水题
很简单的模拟,我是用的优先队列.不多说,上代码(这是bjfuoj的,hdu的要稍改一下): /* * Author : ben */ #include <cstdio> #include ...
- Android FragmentActivity+viewpager的使用
使用场景,打算设计一个“底部菜单栏+其余可滑动的页面”的简单的功能. package com.lanyuweng.mibaby; import android.content.Intent; impo ...
- ubuntu下php5扩展mysqli
看网上说的都是自己编译源码,试了一下其实这样就可以了 sudo apt-get instal php5-mysql
- 纯CSS制作冒泡提示框
来源:http://www.ido321.com/1214.html 前两天翻译了一篇文章,关于利用css的border属性制作基本图形:http://www.ido321.com/1200.html ...
- sgu 194 Reactor Cooling(有容量上下界的无源无汇可行流)
[题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20757 [题意] 求有容量上下界的无源无汇可行流. [思路] ...
- ini_set /ini_get函数功能-----PHP
配置PHP环境时,我们记得最初的一步就是修改php.ini文件,但是当我们在虚拟机中运行脚本,或者是我们因为其他的原因没有修改php.ini的权限时,我们该怎么办? ini_set()函数提供了在脚本 ...
- 【bzoj3233】【ahoi2013】找硬币
题意: 求确定n种货币面额x1..xn满足 x1=1 且xi为xj的整数倍(i>j) 给定n个物品价格ai 求使用上面货币最少需要硬币数(不能找零) 题解: 动态规划 听说网上的题解都是搜索的做 ...
- HDU-4751 Divide Groups 染色问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 题意:有n个人,每个人都认识一些人,要求把他们分成两个集合,使得两个集合中的人都相符两两认识. ...
- redis的hashes类型
redis hash 是一个string类型的field和value 的映射表.它的添加.删除操作都是O(1) . hash特别适合用于存储对象.相较于将对象的每个字段存成单个string类型 . 将 ...
- cocos2d-x图片变灰或者变亮
//根据现有CCSprite,变亮和变灰 CCSprite* FlyLeaf::graylightWithCCSprite(CCSprite* oldSprite,bool isLight) { ...