###### 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); } } }; ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up