class Solution {
public:
vector<int> preorderTraversal(TreeNode* root){
vector<int> res;
if(!root) {
return res;
}
stack<TreeNode*> bucket;
bucket.push(root);
while(!bucket.empty()) {
TreeNode* cur = bucket.top();
res.push_back(cur->val);
bucket.pop();
if(cur->right) {
bucket.push(cur->right);
}
if(cur->left) {
bucket.(cur->left);
}
}
return res;
}
};
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res;
if(!root) return res;
stack<TreeNode*> s;
s.push(root);
while(!s.empty()) {
TreeNode* temp=s.top();
res.push_back(temp->val);
s.top();
if(temp->right) {
s.push(temp->right);
}
if(temp->left) {
s.push(temp->left);
}
}
return res;
}
};
不知道为什么。。。第一段运行正常,第二段力扣上就显示超时了。。。。