给出一棵二叉树,返回其中序遍历
样例
给出二叉树 {1,#,2,3}
,
1 \ 2 / 3
返回 [1,3,2]
.
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/*
* @param root: A Tree
* @return: Inorder in ArrayList which contains node values.
*/
vector<int> inorderTraversal(TreeNode * root) {
vector<int> tmp;
if(root==NULL) return tmp;
stack<TreeNode*> s;
TreeNode* cur=root;
while(!s.empty()||cur!=NULL){
if(cur!=NULL){
s.push(cur);
cur=cur->left;
}else{
cur=s.top();
tmp.push_back(cur->val);
s.pop();
cur=cur->right;
}
}
return tmp;
}
};