二叉树的中序遍历非递归
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 * @return int整型vector */ vector<int> inorderTraversal(TreeNode* root) { // write code here // 直接奥义非递归 vector<int> res; if(root == nullptr) return res; stack<TreeNode*> s; while(root || s.size()) { while(root) { s.push(root); root = root->left; } root = s.top(); s.pop(); res.push_back(root->val); root = root->right; } return res; } };