二叉樹非遞歸中序遍歷node
class Solution { public: /** * @param root: A Tree * @return: Inorder in ArrayList which contains node values. */ vector<int> inorderTraversal(TreeNode * root) { // write your code here stack<TreeNode*> stack; vector<int> res; TreeNode* curt = root; while(curt != nullptr || !stack.empty()) { while(curt != nullptr) { stack.push(curt); curt = curt->left; } curt = stack.top(); stack.pop(); res.push_back(curt->val); curt = curt->right; } return res; } };