-
[LeetCode] 144. Binary Tree Preorder Traversal c++Coding Test/LeetCode 2022. 12. 23. 17:58728x90
- 문제
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'Coding Test > LeetCode' 카테고리의 다른 글
[LeetCode] 283. Move Zeroes c++ (0) 2022.12.24 [LeetCode] 606. Construct String from Binary Tree c++ (0) 2022.12.24 [LeetCode] 189. Rotate Array c++ (0) 2022.12.23 [LeetCode] 977. Squares of a Sorted Array c++ (0) 2022.12.23 [LeetCode] 145. Binary Tree Postorder Traversal c++ (0) 2022.12.22