Coding Test/LeetCode
[LeetCode] 144. Binary Tree Preorder Traversal c++
owls
2022. 12. 23. 17:58
728x90
- 문제
Given the root of a binary tree, return the preorder traversal of its nodes' values.
Input: root = [1,null,2,3]
Output: [1,2,3]
- 문제 해결
class Solution {
private:
void preorder(TreeNode* curr, vector<int>& vec){
if(!curr){
return;
}
vec.push_back(curr->val);
preorder(curr->left, vec);
preorder(curr->right, vec);
return;
}
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> vec;
preorder(root, vec);
return vec;
}
};
728x90