# 思路
inorder
順序為 左中右
用 stack
儲存節點,先找到最左的 node
,再依序加入 ret
# 參考程式碼
/** | |
* Definition for a binary tree node. | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode() : val(0), left(nullptr), right(nullptr) {} | |
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | |
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | |
* }; | |
*/ | |
static auto fast_io = [] | |
{ | |
ios::sync_with_stdio(false); | |
cout.tie(nullptr); | |
cin.tie(nullptr); | |
return 0; | |
}(); | |
class Solution { | |
public: | |
vector<int> inorderTraversal(TreeNode* root) | |
{ | |
vector<int> ret; | |
stack<TreeNode*> S; | |
TreeNode* cur; | |
while(root || !S.empty()) | |
{ | |
while(root) S.push(root), root = root->left; | |
cur = S.top(), S.pop(); | |
ret.push_back(cur->val); | |
root = cur->right; | |
} | |
return ret; | |
} | |
}; |