populating-next-right-pointers-in-each-node-ii leetcode C++
2024-10-19 14:43:11
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
C++
/**
* 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){
while(root){
TreeLinkNode L(-1);
TreeLinkNode *f;
TreeLinkNode *p;
f = &L;
p = root;
while(p){
if(p->left != NULL){
f->next = p->left;
f = f->next;
}
if(p->right != NULL){
f->next = p->right;
f = f->next;
}
p = p->next;
}
root = L.next;
}
}
};
最新文章
- 手把手教你写一个RN小程序!
- sass接触
- PostgreSQL 添加自定义变量
- 锋利的jQuery——19个jQuery 常用片段整理
- Python 前端之JS
- pmcyg 1.0 发布,Cygwin 安装包创建工具
- Facebook Hacker Cup 2014 Qualification Round 竞赛试题 Square Detector 解题报告
- dp核心问题研究-从入门到放弃
- CamShift算法
- 用DateTime.ToString(string format)输出不同格式的日期
- [未完成][Mooc]关于Linxu的总结(一)
- ZOJ-2365 Strong Defence 贪心,BFS
- 【行为型】Mediator模式
- ERROR 1130: Host xxx is not allowed to connect to this MySQL server
- centos 日常操作指令
- listview 去掉header和footer中的分割线
- mysql时间戳与日期格式的相互转换
- 记小白的一次基于vue+express+mongodb个人站开发
- Sharding-jdbc实现分库分表
- MyBatis-Plugins 的创建流程与执行顺序