给定一个二叉树,前序输出
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
#include<stack>
class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
vector<int> temp_v;
if(root==NULL) return temp_v;
stack<TreeNode*> temp_stack;
temp_stack.push(root);
while(!temp_stack.empty())
{
TreeNode* temp_p=temp_stack.top();
temp_stack.pop();
temp_v.push_back(temp_p->val);
if(temp_p->right!=NULL) temp_stack.push(temp_p->right);
if(temp_p->left!=NULL) temp_stack.push(temp_p->left);
}
return temp_v;
}
};
转载请注明原文地址: https://ju.6miu.com/read-1309932.html