由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - leetcode上的populate next node I and II
相关主题
leetcode populating next pointer 2Populating Next Right Pointers in Each Node II
有人同看Populating Next Right Pointers in Each Node II的recursive写法么?G题,把binary tree里面的sibling节点连接起来
Populating Next Right Pointers in Each Node II
相关话题的讨论汇总
话题: node话题: null话题: prev话题: ii
进入JobHunting版参与讨论
1 (共1页)
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;

1 (共1页)
进入JobHunting版参与讨论
相关主题
Populating Next Right Pointers in Each Node II有人同看Populating Next Right Pointers in Each Node II的recursive写法么?
G题,把binary tree里面的sibling节点连接起来Populating Next Right Pointers in Each Node II
leetcode populating next pointer 2
相关话题的讨论汇总
话题: node话题: null话题: prev话题: ii