/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> ret;
if(root == NULL) return ret;
stack<TreeNode*> s;
s.push(root);
while(!s.empty())
{
TreeNode *cur = s.top();
while(cur->left != NULL)
{
cur = cur->left;
s.push(cur);
}
while(!s.empty())
{
cur = s.top();
s.pop();
ret.push_back(cur->val);
if(cur->right != NULL)
{
s.push(cur->right);
break;
}
}
}
return ret;
}
};
[Leetcode]Binary Tree Inorder Traversal without recursion
最新推荐文章于 2021-01-15 13:16:25 发布