版权声明:本文为博主原创文章,未经博主允许不得转载。@ceezyyy11 https://blog.csdn.net/ceezyyy11/article/details/88983976
[LeetCode] 94. Binary Tree Inorder Traversal (C++)
Medium
Share
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]
Follow up: Recursive solution is trivial, could you do it iteratively?
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
/* Recursive implementation */
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
IOT(root,res);
return res;
}
void IOT(TreeNode* root,vector<int> &r) {
if(root) {
IOT(root->left,r);
r.push_back(root->val);
IOT(root->right,r);
}
}
};
/**
* Submission Detail:
* Runtime: 4 ms, faster than 100.00% of C++ online submissions for Binary Tree Inorder Traversal.
* Memory Usage: 9.5 MB, less than 15.33% of C++ online submissions for Binary Tree Inorder Traversal.
*/