leetcode-----144. 二叉树的前序遍历

代码

/*
 * @lc app=leetcode.cn id=144 lang=cpp
 *
 * [144] 二叉树的前序遍历
 */

// @lc code=start
/**
 * 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) {
        vector<int> ans;
        stack<TreeNode*> st;
        while (root || st.size()) {
            while (root) {
                ans.push_back(root->val);
                st.push(root);
                root = root->left;
            }
            root = st.top()->right;
            st.pop();
        }
        return ans;
    }
};
// @lc code=end
posted @ 2020-08-04 17:26  景云ⁿ  阅读(60)  评论(0编辑  收藏  举报