/**
* 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
stack<TreeNode*> st;
vector<int> res;
while (root || !st.empty())
{
while (root)
{
st.push(root);
root = root->left;
}
if (!st.empty())
{
root = st.top();
res.push_back(root->val);
root = root->right;
st.pop();
}
}
return res;
}
};
[Leetcode] Binary Tree Inorder Traversal
最新推荐文章于 2020-07-28 10:43:41 发布