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;
}
}
};

最新文章

  1. 手把手教你写一个RN小程序!
  2. sass接触
  3. PostgreSQL 添加自定义变量
  4. 锋利的jQuery——19个jQuery 常用片段整理
  5. Python 前端之JS
  6. pmcyg 1.0 发布,Cygwin 安装包创建工具
  7. Facebook Hacker Cup 2014 Qualification Round 竞赛试题 Square Detector 解题报告
  8. dp核心问题研究-从入门到放弃
  9. CamShift算法
  10. 用DateTime.ToString(string format)输出不同格式的日期
  11. [未完成][Mooc]关于Linxu的总结(一)
  12. ZOJ-2365 Strong Defence 贪心,BFS
  13. 【行为型】Mediator模式
  14. ERROR 1130: Host xxx is not allowed to connect to this MySQL server
  15. centos 日常操作指令
  16. listview 去掉header和footer中的分割线
  17. mysql时间戳与日期格式的相互转换
  18. 记小白的一次基于vue+express+mongodb个人站开发
  19. Sharding-jdbc实现分库分表
  20. MyBatis-Plugins 的创建流程与执行顺序

热门文章

  1. python中reduce filter map lambda函数
  2. 【TP3.2.3】根据字段统计条数
  3. python刷题第二周
  4. javascript DOM 共同父节点
  5. cmake入门:01 构建一个简单的可执行程序
  6. web带宽估算方法
  7. AT4996-[AGC034F]RNG and XOR【FWT,生成函数】
  8. 全网最新最详细最明白教程之Spring源码搭建,没有之一,超详细
  9. 10.9 Rewrite原理
  10. Longhorn 云原生容器分布式存储 - 故障排除指南