1.二叉树的中序遍历
题目:给出一棵二叉树,返回其中序遍历
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
/**
* @param root: The root of binary tree.
* @return: Inorder in vector which contains node values.
*/
public:
vector<int> inorderTraversal(TreeNode *root) {
// write your code here
vector<TreeNode *> t;
vector<int> res;
while(root != NULL || t.size() != 0) {
while(root != NULL) {
t.push_back(root);
root = root->left;
}
root = t.back();
t.pop_back();
res.push_back(root->val);
root = root->right;
}
return res;
}
};