您的位置:首页 > 其它

144. Binary Tree Preorder Traversal 二叉树的前序遍历

2016-12-11 22:38 537 查看
/**
* Definition for a binary tree node.
* 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*> s;
vector<int> ans;
TreeNode* cur = root;
while(cur || !s.empty()){
if(cur == NULL){
cur = s.top();
s.pop();
cur = cur->right;
}else{
ans.push_back(cur->val);
s.push(cur);
cur = cur->left;
}
}
return ans;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐