标签:tle traversal return block 没有 struct push empty pre
1\2/3
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类
* @return int整型vector
*/
vector<int> preorderTraversal(TreeNode* root) {
// write code here
vector <int> res;
stack<TreeNode*> s;
if (root==NULL){
return res;
}
s.push(root);
while (!s.empty())
{
TreeNode *cur=s.top();
s.pop();
res.push_back(cur->val);
if (cur->right!=NULL)
s.push(cur->right);
if (cur->left !=NULL)
s.push(cur->left);
}
return res;
}
};
leetcode7:binary-tree-preorder-traversal
标签:tle traversal return block 没有 struct push empty pre
原文地址:https://www.cnblogs.com/hrnn/p/13438827.html