---
# System prepended metadata

title: 'LeetCode #94 [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/)'
tags: [Recursion, LeetCode, Easy, Inorder, Tree]

---

###### tags: `LeetCode` `Easy` `Inorder` `Recursion` `Tree`
# LeetCode #94 [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/)
### (Easy)
給定一個二元樹的根節點 root ，返回它的中序遍歷。


---
inorder的規則: 右子樹->根節點->左子樹

創建另一個函式, 當root不為null時, 遞迴呼叫右子樹, 將root的值加入數組, 再遞迴呼叫左子樹。


---
```
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        if(root){
            vector<int> nodes;
            helper(root, nodes);
            return nodes;
        }
        return {};
    }
    void helper(TreeNode* root, vector<int>& nodes){
        if(root){
            helper(root->left,nodes);
            nodes.push_back(root->val);
            helper(root->right,nodes);
        }
    } 
};
```