#include <vector> #include <stack> using namespace std; // Definition for a binary tree node. struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; class Solution { public: vector<int> inorderTraversal(TreeNode* root) { stack<TreeNode*> st; vector<int> inorder; TreeNode* node = root; while (true) { if (node != nullptr) { st.push(node); node = node->left; } else { if (st.empty()) break; node = st.top(); st.pop(); inorder.push_back(node->val); node = node->right; } } return inorder; } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter