之前資料結構寫過一模一樣的題目回憶起來蠻快的
題目敘述:
94. Binary Tree Inorder Traversal
Given the root of a binary tree, return the inorder traversal of its nodes' values.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> num;
vector<int> inorderTraversal(TreeNode* root) {
if (root==nullptr){
return num;//case樹空
}
inorderTraversal(root->left);//用遞迴從左子開始
num.push_back(root->val);
inorderTraversal(root->right);
return num;
}
};