二叉树中序遍历
二叉树非递归中序遍历
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;
}
};

浙公网安备 33010602011771号