作者:cathy522_788 | 来源:互联网 | 2023-06-04 02:58
问题描述:
参考: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 q;vector 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 next = vec[i+1];}vec[vec.size()-1]->next = NULL;vec.clear();}return root;}
};