Binary Inorder Traversal
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> solTree;
if (root == NULL) {
//solTree.push_back(NULL);
return solTree;
}
if (root->left) {
vector<int> leftTree;
leftTree = inorderTraversal(root->left);
solTree = leftTree;
}
solTree.push_back(root->val);
if (root->right) {
vector<int> rightTree;
rightTree = inorderTraversal(root->right);
solTree.insert(solTree.end(), rightTree.begin(), rightTree.end());
}
return solTree;
}
};
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> solTree;
if (root == NULL) {
//solTree.push_back(NULL);
return solTree;
}
if (root->left) {
vector<int> leftTree;
leftTree = inorderTraversal(root->left);
solTree = leftTree;
}
solTree.push_back(root->val);
if (root->right) {
vector<int> rightTree;
rightTree = inorderTraversal(root->right);
solTree.insert(solTree.end(), rightTree.begin(), rightTree.end());
}
return solTree;
}
};
No comments:
Post a Comment