# 【LeetCode】 94. Binary Tree Inorder Traversal ## Description > Given a binary tree, return the inorder traversal of its nodes' values. > 給一棵二元樹,回傳這棵樹的中序遍歷。 ## Example: ``` Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,3,2] ``` ## Solution * 二元樹中的基本題。使用遞迴跑每個節點,有左邊就往左邊走,然後加入自己,再往右邊走。 * 如果想得到前序或後序,只需要改變下方code中Line`17`~`19`的順序即可。 * [144. Binary Tree Preorder Traversal](https://hackmd.io/s/H1wDu2-p4) * [145. Binary Tree Postorder Traversal](https://hackmd.io/s/BkETP3WaN) ### Code ```C++=1 /** * 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: void GetInorder(TreeNode* root, vector<int>& v) { if(root) { GetInorder(root->left,v); v.push_back(root->val); GetInorder(root->right,v); } } vector<int> inorderTraversal(TreeNode* root) { vector<int> v; GetInorder(root, v); return v; } }; ``` ###### tags: `LeetCode` `C++`
×
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