中序遍历二叉树
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> result;
if(root == NULL) {
return result;
}
stack<TreeNode *> vstack;
TreeNode *cur = root;
do {
while(cur != NULL) {
vstack.push(cur);
cur = cur->left;
}
cur = vstack.top()->right;
result.push_back(vstack.top()->val);
vstack.pop();
} while(!vstack.empty() || cur != NULL);
return result;
}
};