LeetCode_Populating Next Right Pointers in Each Node II
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
/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
while(root){
TreeLinkNode *head = NULL;
TreeLinkNode *pre = NULL;
for(;root;root = root->next){
if(!head) head = root->left ? root->left : root->right; if(root->left){
if(pre)pre->next = root->left;
pre = root->left;
} if(root->right){
if(pre) pre->next = root->right;
pre = root->right;
}
}// end of for
root = head;
} // end of while
}
};
LeetCode_Populating Next Right Pointers in Each Node II的更多相关文章
- Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II
题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...
- 【leetcode】Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
- 29. Populating Next Right Pointers in Each Node && Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node OJ: https://oj.leetcode.com/problems/populating-next-rig ...
- Populating Next Right Pointers in Each Node,Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node Total Accepted: 72323 Total Submissions: 199207 Difficul ...
- leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II
leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- Leetcode 树 Populating Next Right Pointers in Each Node II
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...
- 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】117. Populating Next Right Pointers in Each Node II (2 solutions)
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
- [Leetcode Week15]Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...
随机推荐
- CentOS下Qt窗口透明效果失效,成黑色背景的问题
一.问题 Linux系统下Qt窗口的透明效果成了黑色,但同样的代码在windows机子上有透明效果,主要是修改系统的配置,仅在centos6.3及其以上版本实验过.其他系统可以相应配置. 二.问题运行 ...
- 由于 UNION ALL Chinese_PRC_CI_AS”之间的排序规则冲突,值的排序规则未经解析
由于不同的表之间的排序规则不一样,在归并集合的 时候会出现排序问题. 只要在查询的列后面 声明结果列的排序规则保持一致即可: SELECT b0.[CardCode] collate SQL_Lat ...
- 一些pyhon的学习资料
一直没有时间学习python,都说python语法简洁优美.但是我看它的语法还是不爽啊,没C/C++好阅读.但是C/C++又必须了解底层的计算机模型.着实会门槛高一些.特别是C++,对于我来说简直就是 ...
- Leetcode:LRUCache四个版本实现
题目 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...
- js设计模式系列之(一)请节约你的请求-代理模式
What’s the proxy pattern? 代理模式其实就是将违反单一性原则的类给抽离出来,尽量满足开放和封闭的原则. 相当于一个类的行为只是一种,但是你可以给这个类添加额外的行为.比如: 一 ...
- 70个经典的 Shell 脚本面试问题
转载自:http://www.imooc.com/article/1131 1) 如何向脚本传递参数 ? ./script argument 例子: 显示文件名称脚本 ./show.sh file1. ...
- Makefile学习(三)执行make
9 执行make 一般方法:make. 某些情况:1.可能需要使用make更新一部分过时文件而不是全部 2.需要使用另外的编译器或者重新定义编译选项 3.只需要查看哪些文件被修改,不需要重新编译 所以 ...
- 畅通project
原文请訪问:p=174">http://xiaoshig.sinaapp.com/?p=174 畅通project Time Limit:2000MS Memory Limit ...
- 技术路线 vs 技术管理路线
最近因为要给刚毕业的学生做一次演讲,所以就职业发展这类话题先以写博客的形式做一些思考,希望届时能给同学们带来质量更高的内容.我在<驾驭你的"职场布朗运动">一文中谈了2 ...
- hashtable 和dictionary
hashtable 通过 key 和value 进行访问 不是 通过 索引访问 对类型没有强制规定 ,所以类型危险 容易出错 无效的key时 会返回空 dictionary 与hashtable 相区 ...