# 94. Binary Tree Inorder Traversal (Medium)

## 1.問題&#x20;

* 給予一個binary tree, 回傳一個inorder traversal的node's序列&#x20;

![](https://901207480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGKoChvN9am4__HCIRK%2F-LHBjHH6W0vptgZB9OR0%2F-LHBkPZGlLgZOEus49J-%2F2018071202.jpg?alt=media\&token=0b8b98a2-f543-4821-b4bb-6d6011a6316a)

## 2.想法&#x20;

* [inorder traversal](https://jenhsuan.gitbook.io/algorithm/notes-of-algorithms/binary-tree-traversal) + recursive

## &#x20;3.程式碼&#x20;

```
/**
 * Definition for a binary tree node.
 * 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) {
        vector<int> v;
        if (root) {
            inOrder(root, v);
        }
        return v;
    }
private:
    void inOrder(TreeNode* root, vector<int>& v) {
        if (root) {
            if (root->left) {
                inOrder(root->left, v);   
            }   
            v.push_back(root->val);
            if (root->right) {
                inOrder(root->right, v);   
            }    
        }
    }
};
```

## 4.Performance

![](https://901207480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGKoChvN9am4__HCIRK%2F-LHBjHH6W0vptgZB9OR0%2F-LHBk-wUdr7kFIydgvVD%2F2018071201.jpg?alt=media\&token=706092a9-e11b-490e-af8a-56f555962af1)
