g*********e 发帖数: 14401 | 1 这题I是binary tree,不知bst有什么可以利用的性质?我觉得i ii都一样啊
我的code
class Solution {
public:
void connect(TreeLinkNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
queue q;
if(!root)
return;
q.push(root);
q.push(NULL);
TreeLinkNode* prev=NULL;
while(!q.empty()){
TreeLinkNode* node=q.front();
q.pop();
if(!node){
if(q.empty())
break;
q.push(NULL);
prev=NULL;
continue;
}
if(node->left)
q.push(node->left);
if(node->right)
q.push(node->right);
if(prev)
prev->next=node;
node->next=NULL;
prev=node;
}
}
}; |
h*********o 发帖数: 230 | 2 这种做法, 对是不是complete没影响。
但是要求 O(1) 空间就不一样了。。
【在 g*********e 的大作中提到】 : 这题I是binary tree,不知bst有什么可以利用的性质?我觉得i ii都一样啊 : 我的code : class Solution { : public: : void connect(TreeLinkNode *root) { : // Start typing your C/C++ solution below : // DO NOT write int main() function : queue q; : if(!root) : return;
|
c********t 发帖数: 5706 | 3 试试O(1)空间解吧。第一题因为是perfect bt, 更容易些。
【在 g*********e 的大作中提到】 : 这题I是binary tree,不知bst有什么可以利用的性质?我觉得i ii都一样啊 : 我的code : class Solution { : public: : void connect(TreeLinkNode *root) { : // Start typing your C/C++ solution below : // DO NOT write int main() function : queue q; : if(!root) : return;
|