1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
class Solution { public: vector<int> preorderTraversal(TreeNode* root) { vector<int> res; stack<TreeNode*> stk; while(root || stk.size()) { while(root) { res.push_back(root->val); stk.push(root); root = root->left; } root = stk.top()->right; stk.pop(); } return res; } };
|