二叉树的中序遍历
class Solution { public: vector<int> inorderTraversal(TreeNode *root) { std::stack<TreeNode *> stack; std::vector<int> res; while (root != nullptr || !stack.empty()) { while (root != nullptr){ stack.push(root); root = root->left; } root = stack.top(); stack.pop(); res.push_back(root->val); root = root->right; } return res; } };
浙公网安备 33010602011771号