Given a binary tree, return the inorder traversal of its nodes' values.
Example:
Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,3,2]
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
#define MaxSize 100
typedef struct Stack{
TreeNode* data[MaxSize];
int top;
}Stack;
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> vector;
if (root == NULL) {
return vector;
}
Stack stack;
stack.top = -1;
//Root gets into stack
stack.data[++(stack.top)] = root;
TreeNode* q;
int visit = 0;
while (stack.top != -1) {
q = stack.data[stack.top];
if (q->left && visit == 0) {
stack.data[++(stack.top)] = q->left;
}else{
q = stack.data[(stack.top--)];
visit = 1;
vector.push_back(q->val);
if (q->right) {
stack.data[++(stack.top)] = q->right;
visit = 0;
}
}
}
return vector;
}
};