leetcode_c++:树:Populating Next Right Pointers in Each Node II(117)
来源:互联网 发布:淘宝香火符咒西极飞飞 编辑:程序博客网 时间:2024/06/01 10:32
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
class Solution {public: void connect(TreeLinkNode *root) { TreeLinkNode header(0); header.next = root; TreeLinkNode *cur, *prev; while (header.next) { // current header's link is already populated // following will make a new children-link to header cur = header.next; prev = &header; header.next = nullptr; for (; cur; cur = cur->next) { if (cur->left != nullptr) { prev->next = cur->left; prev = prev->next; } if (cur->right != nullptr) { prev->next = cur->right; prev = prev->next; } } } }};
0 0
- leetcode_c++:树:Populating Next Right Pointers in Each Node II(117)
- LeetCode 117 Populating Next Right Pointers in Each Node II
- LeetCode: Populating Next Right Pointers in Each Node II [117]
- LeetCode 117 Populating Next Right Pointers in Each Node II
- [leetcode 117] Populating Next Right Pointers in Each Node II
- [LeetCode 117]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 117
- leetcode[117]:Populating Next Right Pointers in Each Node II
- Leetcode #117 Populating Next Right Pointers in Each Node II
- LeetCode 117: Populating Next Right Pointers in Each Node II
- 117Populating Next Right Pointers in Each Node II
- LeetCode 117:Populating Next Right Pointers in Each Node II
- leetcode 117 Populating Next Right Pointers in Each Node II
- Leetcode 117 Populating Next Right Pointers in Each Node II
- LeetCode 117 Populating Next Right Pointers in Each Node II
- Leetcode-117: Populating Next Right Pointers in Each Node II
- Leetcode 树 Populating Next Right Pointers in Each Node II
- iOS 通讯录操作 删除联系人 以及联系人多个号码 如何删除其中一个号码
- [iOS]百分比编码
- 选项卡实现(使用this)
- Python中文件读取方法read(),readline(),readlines()的区别
- Swift-->Http网络请求(NSURLSession, Alamofire)
- leetcode_c++:树:Populating Next Right Pointers in Each Node II(117)
- hdu5833 Zhu and 772002
- Interllij 自定义模板
- savedInstanceState
- Ubuntu 下 网易云音乐使用体验
- 解决nagios报错:./stdio.h:456:1: error: 'gets' undeclared here (not in a function)的问题
- 二、谈谈算法
- JavaScript性能优化技巧之函数节流
- 写开源项目步骤(上传github、发布到jcenter、录屏gif)