Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1 \ 2 / 3
return [1,3,2]
.
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
vector<int> inorderTraversal(TreeNode *root) {
vector<int>ResultVectorPsotorder;
stack<TreeNode*>StackNode;
if (root == NULL)
return ResultVectorPsotorder;
TreeNode*CurNode = root;
while (1){
while (CurNode!=NULL){
StackNode.push(CurNode);
CurNode = CurNode->left;
}
if (StackNode.empty())
break;
CurNode = StackNode.top();
ResultVectorPsotorder.push_back(CurNode->val);
StackNode.pop();
CurNode = CurNode->right;
}
return ResultVectorPsotorder;
}