leetcode 144. Binary Tree Preorder Traversal

来源:互联网 发布:淘宝唱片店正版 编辑:程序博客网 时间:2024/06/11 20:42
class Solution {public:vector<int> preorderTraversal(TreeNode* root) {if (root == nullptr){return{};}vector<int> res;stack<TreeNode*>stack;stack.push(root);while (!stack.empty()){auto p = stack.top();stack.pop();while (p){res.push_back(p->val);if (p->right){stack.push(p->right);}p = p->left;}}return res;}};

0 0