- LeetCode #116 #117 2018-08-07
- 116. Populating Next Right Point
- 116. Populating Next Right Point
- 116. Populating Next Right Point
- 116. Populating Next Right Point
- 116. Populating Next Right Point
- 116. Populating next right point
- 116. Populating Next Right Point
- 116. Populating Next Right Point
- 116. Populating Next Right Point
题目链接
https://leetcode.com/problems/populating-next-right-pointers-in-each-node/
代码
class Solution {
public:
Node* connect(Node* root) {
if (root == NULL) {
return NULL;
}
queue<Node*> que;
que.push(root);
que.push(NULL);
Node* pre = NULL, *cur;
while (true) {
cur = que.front();
que.pop();
if (pre != NULL) {
pre->next = cur;
}
pre = cur;
if (cur == NULL) {
if (que.empty()) {
break;
} else {
pre = NULL;
que.push(NULL);
}
} else {
if (cur->left != NULL) {
que.push(cur->left);
}
if (cur->right != NULL) {
que.push(cur->right);
}
}
}
return root;
}
};
网友评论