[leetcode]Populating Next Right Pointers in Each Node II
来源:互联网 发布:java数组的长度单位 编辑:程序博客网 时间:2024/05/07 02:26
class Solution {
public:
void insert_node(TreeLinkNode *&tail, TreeLinkNode *p) {
p->next = NULL;
tail->next = p;
tail = p;
}
TreeLinkNode *delete_node(TreeLinkNode *fake_head, TreeLinkNode *&tail) {
TreeLinkNode *p;
if (fake_head->next == tail) {
p = fake_head->next;
fake_head->next = NULL;
tail = fake_head;
} else {
p = fake_head->next;
fake_head->next = p->next;
}
return p;
}
void connect(TreeLinkNode *root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
TreeLinkNode *fake_head, *tail;
fake_head = tail = new TreeLinkNode(0);
if (root != NULL) insert_node(tail, root);
int cnt = 1;
while (fake_head->next != NULL) {
int new_cnt = 0;
for (int i = 1; i <= cnt; i++) {
TreeLinkNode *front = delete_node(fake_head, tail);
if (i < cnt)
front->next = fake_head->next;
else
front->next = NULL;
if (front->left != NULL) {
new_cnt++;
insert_node(tail, front->left);
}
if (front->right != NULL) {
new_cnt++;
insert_node(tail, front->right);
}
}
cnt = new_cnt;
}
delete fake_head;
}
};
public:
void insert_node(TreeLinkNode *&tail, TreeLinkNode *p) {
p->next = NULL;
tail->next = p;
tail = p;
}
TreeLinkNode *delete_node(TreeLinkNode *fake_head, TreeLinkNode *&tail) {
TreeLinkNode *p;
if (fake_head->next == tail) {
p = fake_head->next;
fake_head->next = NULL;
tail = fake_head;
} else {
p = fake_head->next;
fake_head->next = p->next;
}
return p;
}
void connect(TreeLinkNode *root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
TreeLinkNode *fake_head, *tail;
fake_head = tail = new TreeLinkNode(0);
if (root != NULL) insert_node(tail, root);
int cnt = 1;
while (fake_head->next != NULL) {
int new_cnt = 0;
for (int i = 1; i <= cnt; i++) {
TreeLinkNode *front = delete_node(fake_head, tail);
if (i < cnt)
front->next = fake_head->next;
else
front->next = NULL;
if (front->left != NULL) {
new_cnt++;
insert_node(tail, front->left);
}
if (front->right != NULL) {
new_cnt++;
insert_node(tail, front->right);
}
}
cnt = new_cnt;
}
delete fake_head;
}
};
- 【LeetCode】Populating Next Right Pointers in Each Node Populating Next Right Pointers in Each Node II
- LeetCode Populating Next Right Pointers in Each Node II
- [Leetcode] Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node II
- leetcode 79: Populating Next Right Pointers in Each Node II
- [Leetcode] Populating Next Right Pointers in Each Node ii
- leetcode Populating Next Right Pointers in Each Node II
- 【leetcode】Populating Next Right Pointers in Each Node II
- [Leetcode]Populating Next Right Pointers in Each Node II
- [leetcode] Populating Next Right Pointers in Each Node II
- LeetCode-Populating Next Right Pointers in Each Node II
- LeetCode - Populating Next Right Pointers in Each Node II
- [LeetCode]Populating Next Right Pointers in Each Node II
- leetcode Populating Next Right Pointers in Each Node II
- LeetCode | Populating Next Right Pointers in Each Node II
- [leetcode]Populating Next Right Pointers in Each Node II
- 【LeetCode OJ】Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node II
- 9) 常用函数对象functionobject [原创,泛型编程,自由下载转载,需注明出处]
- 电脑操作技术
- Foundation: Balanced Tree Search
- 【OSGi】OSGi如何解决Java模块化的不足
- C++类中拷贝构造函数详解
- [leetcode]Populating Next Right Pointers in Each Node II
- Android soundPool 不能播放声音问题
- hduTHE MATRIX PROBLEM(差分约束)
- java基础入门---- LineNumberReader1
- 最长公共子序列(LCS)
- java基础入门----MyLineNumberReader1
- TXT导入到数据库
- oracle 位运算
- [leetcode]Distinct Subsequences