中央空调的风扇自己打开了??# Living
x*0
1 楼
class Solution {
private:
TreeNode *pre;
public:
Solution() : pre(NULL) {}
void flatten(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
//static TreeNode* pre = NULL;
if (root != NULL) {
flatten(root->right);
flatten(root->left);
root->right = pre;
root->left = NULL;
pre = root;
}
}
};
各位帮我看看这段代码有什么问题?我自己测试leetcode给出的数据,没有问题。但是
就是过不了。
如果改成如下写法:
class Solution {
public:
void flatten(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
TreeNode* temp = NULL;
postorder(root, temp);
}
private:
void postorder(TreeNode* node , TreeNode*& temp) {
if (node != NULL){
postorder(node->right,temp);
postorder(node->left,temp);
node->right = temp;
temp = node;
node->left = NULL;
}
}
};
就能通过。
这样是不是可以推测,leetocde是通过如下方式测试:
Solution sl;
sl.flatten(test1);
sl.flatten(test2);
....
private:
TreeNode *pre;
public:
Solution() : pre(NULL) {}
void flatten(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
//static TreeNode* pre = NULL;
if (root != NULL) {
flatten(root->right);
flatten(root->left);
root->right = pre;
root->left = NULL;
pre = root;
}
}
};
各位帮我看看这段代码有什么问题?我自己测试leetcode给出的数据,没有问题。但是
就是过不了。
如果改成如下写法:
class Solution {
public:
void flatten(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
TreeNode* temp = NULL;
postorder(root, temp);
}
private:
void postorder(TreeNode* node , TreeNode*& temp) {
if (node != NULL){
postorder(node->right,temp);
postorder(node->left,temp);
node->right = temp;
temp = node;
node->left = NULL;
}
}
};
就能通过。
这样是不是可以推测,leetocde是通过如下方式测试:
Solution sl;
sl.flatten(test1);
sl.flatten(test2);
....