LeetCode 94. Binary Tree Inorder Traversal

2020-02-11 12:19:14 浏览数 (1)

题目

代码语言:javascript复制
/**
* 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 {
public:

TreeNode* s[100005];
vector<int> inorderTraversal(TreeNode* root) {


TreeNode* term = root;
vector<int> ans;
if(root==NULL)
return ans;
int pos=0;

s[pos  ] = term;

while(pos!=0)
{
TreeNode* temp = s[pos-1];
TreeNode* temp2 = s[pos-1];
if(temp->left!=NULL&&temp->left->val!=-99999999)
{
s[pos  ]=temp->left;
continue;
}

ans.push_back(s[pos-1]->val);
pos--;
temp2->val=-99999999;

if(temp->right!=NULL&&temp->right->val!=-99999999)
{
s[pos  ]=temp->right;
continue;
}



}

return ans;

}
};

0 人点赞