/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
stack<TreeNode*> node_list;
vector<int> s;
if(root == NULL){
return s;
}
TreeNode* cur ;
node_list.push(root);
while(!node_list.empty()){
cur = node_list.top();
s.push_back(cur->val);
node_list.pop();
if(cur->right != NULL){
node_list.push(cur->right);
}
if(cur->left != NULL){
node_list.push(cur->left);
}
}
return s;
}
};
【面试准备】letcode-Binary Tree Preorder Traversal
最新推荐文章于 2014-11-25 00:15:27 发布