Coding Test
-
[LeetCode] 144. Binary Tree Preorder Traversal c++Coding Test/LeetCode 2022. 12. 23. 17:58
문제 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& vec){ if(!curr){ return; } vec.push_back(curr->val); preorder(curr->left, vec); preorder(curr->right, vec); return; } public: vector preorderTraversal(TreeNode* root) { vector vec; preorder(root, v..
-
[LeetCode] 189. Rotate Array c++Coding Test/LeetCode 2022. 12. 23. 15:10
문제 Given an array, rotate the array to the right by k steps, where k is non-negative. Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] 문제 해결 rotate는 맨 뒤 원소가 맨 앞원소로 이동된다. k 개수 만큼 nums맨뒤의 원소가 맨앞으로 rotate된다. 회전한 원소들이 기존 순서를 유지하여 rotate되는 것을 ..
-
[LeetCode] 977. Squares of a Sorted Array c++Coding Test/LeetCode 2022. 12. 23. 14:10
문제 Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order. Input: nums = [-4,-1,0,3,10] Output: [0,1,9,16,100] Explanation: After squaring, the array becomes [16,1,0,9,100]. After sorting, it becomes [0,1,9,16,100]. 주어진 배열의 원소들을 제곱한뒤 오름차순으로 재정렬하여 답을 구하는 문제이다. 문제 해결 sort함수를 사용한 문제 풀이이다. class Solution { public: vect..
-
[LeetCode] 145. Binary Tree Postorder Traversal c++Coding Test/LeetCode 2022. 12. 22. 18:12
문제 Given the root of a binary tree, return the postorder traversal of its nodes' values. Input: root = [1,null,2,3] Output: [3,2,1] 문제 해결 vector postorderTraversal(TreeNode* root) { stack stk; if(root){ stk.push(root); } vector ans; while(stk.size()){ auto p = stk.top(); if(p == nullptr){ stk.pop(); ans.push_back(stk.top()->val); stk.pop(); continue; } stk.push(nullptr); if(p->right){ stk.push(p..
-
[LeetCode] 872. Leaf-Similar Trees c++Coding Test/LeetCode 2022. 12. 22. 15:33
문제 Consider all the leaves of a binary tree, from left to right order, the values of those leaves form a leaf value sequence. For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8). Two binary trees are considered leaf-similar if their leaf value sequence is the same. Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar. Input:..
-
[LeetCode] 965. Univalued Binary Tree c++Coding Test/LeetCode 2022. 12. 22. 13:52
문제 A binary tree is uni-valued if every node in the tree has the same value. Given the root of a binary tree, return true if the given tree is uni-valued, or false otherwise. Input: root = [1,1,1,1,1,null,1] Output: true tree node들은 모두 같은 값인지 확인하는 문제이다. 문제 해결 class Solution { private: bool Recur(TreeNode* curr, int nValue){ if(!curr){ return true; } if(curr->val != nValue){ return false; } retur..
-
[LeetCode] 463. Island Perimeter c++Coding Test/LeetCode 2022. 12. 22. 12:56
문제 You are given row x col grid representing a map where grid[i][j] = 1 represents land and grid[i][j] = 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells). The island doesn't have "lakes", meaning the water inside isn't connected to the water a..
-
[LeetCode] 559. Maximum Depth of N-ary Tree c++Coding Test/LeetCode 2022. 12. 20. 17:55
문제 Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples). Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13..