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
原文地址:https://www.cnblogs.com/clown9804/p/13434595.html