94. Binary Tree Inorder Traversal 發表於 2023-02-13 | 分類於 leetcode problemsolutionoption 1 - dfs12345678910111213141516class Solution {public: void inorder(TreeNode* root, vector<int>& ret){ if(!root) return; inorder(root->left, ret); ret.push_back(root->val); inorder(root->right, ret); } vector<int> inorderTraversal(TreeNode* root) { vector<int> ret; inorder(root, ret); return ret; }}; option 2 - iterative + stack123456789101112131415161718192021class Solution {public: vector<int> inorderTraversal(TreeNode* root) { stack<TreeNode*> sta; TreeNode *p = root; vector<int> ret; while (p || !sta.empty()) { while (p) { // L sta.push(p); p=p->left; } p = sta.top(); sta.pop(); ret.push_back(p->val); // V p = p->right; // R } return ret; }};