问题描述:
参考:116
唯一不同的是:这个树不完美,有空洞。
执行结果:
代码描述:
与116题的代码没有任何区别。不知道是我处理的问题,还是问题本身的问题。
/*
// Definition for a Node.
class Node {
public:
int val;
Node* left;
Node* right;
Node* next;
Node() {}
Node(int _val, Node* _left, Node* _right, Node* _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
};
*/
class Solution {
public:
Node* connect(Node* root) {
if(root == NULL) return root;
queue<Node*> q;
vector<Node*> vec;
q.push(root);
root->next = NULL;
Node* temp = NULL;
int count = 0;
while(!q.empty())
{
count = q.size();
while(count > 0)
{
temp = q.front();
q.pop();
if(temp->left != NULL)
{
q.push(temp->left);
vec.push_back(temp->left);
}
if(temp->right != NULL)
{
q.push(temp->right);
vec.push_back(temp->right);
}
--count;
}
if(vec.size() == 0) return root;
for(int i = 0; i < vec.size() - 1; ++i)
{
vec[i]->next = vec[i+1];
}
vec[vec.size()-1]->next = NULL;
vec.clear();
}
return root;
}
};