Coding Test
-
[LeetCode] 637. Average of Levels in Binary Tree c++Coding Test/LeetCode 2022. 12. 20. 14:52
문제 Given the root of a binary tree, return the average value of the nodes on each level in the form of an array. Answers within 10^-5 of the actual answer will be accepted. Input: root = [3,9,20,null,null,15,7] Output: [3.00000,14.50000,11.00000] Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11. Hence return [3, 14.5, 11]. 같은 depth에 있는 node들의 평균 값..
-
[LeetCode] 104. Maximum Depth of Binary Tree c++Coding Test/LeetCode 2022. 12. 20. 12:06
문제 Given the root of a binary tree, return its maximum depth. A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Input: root = [3,9,20,null,null,15,7] Output: 3 트리의 maximum depth를 구하는 문제이다. 문제 해결 int maxDepth(TreeNode* root) { if (!root) { return 0; } int maxLeft = maxDepth(root->left); int maxRight = maxDepth(root->righ..
-
[LeetCode] 94. Binary Tree Inorder Traversal c++Coding Test/LeetCode 2022. 12. 19. 21:36
문제 Given the root of a binary tree, return the inorder traversal of its nodes' values. Input: root = [1,null,2,3] Output: [1,3,2] 트리 순회 알고리즘 중 중위 순회(Inorder Traversal) 를 사용하여 방문한 순서대로 노드 값을 출력하는 문제이다. Inorder Traversal(중위 순회) 란? (left, v); v.push_back(root->val); visitInorder(root->right, v); } } vector inorderTraversal(TreeNode* root) { vector v; visitInorder(root, v); return v; } 다른 방법 class S..
-
[LeetCode] 226. Invert Binary Tree c++Coding Test/LeetCode 2022. 12. 19. 19:37
문제 Given the root of a binary tree, invert the tree, and return its root. Input: root = [4,2,7,1,3,6,9] Output: [4,7,2,9,6,3,1] 부모 노드를 기준으로 자식 노드의 좌우 대칭을 바꾸는 문제입니다. 문제 해결 class Solution { public: TreeNode* invertTree(TreeNode* root) { if (!root) return root; swap(root->left, root->right); invertTree(root->left); invertTree(root->right); return root; } }; swap()함수는 algorithm에 내장된 STL함수입니다. swap()..
-
[LeetCode] 1022. Sum of Root To Leaf Binary Numbers c++Coding Test/LeetCode 2022. 12. 19. 18:58
문제 You are given the root of a binary tree where each node has a value 0 or 1. Each root-to-leaf path represents a binary number starting with the most significant bit. For example, if the path is 0 -> 1 -> 1 -> 0 -> 1, then this could represent 01101 in binary, which is 13. For all leaves in the tree, consider the numbers represented by the path from the root to that leaf. Return the sum of the..
-
[LeetCode] 590. N-ary Tree Postorder Traversal c++Coding Test/LeetCode 2022. 11. 29. 18:51
문제 Given the root of an n-ary tree, return the postorder traversal of its nodes' values. Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples) Example 1: Input: root = [1,null,3,2,4,null,5,6] Output: [5,6,3,2,4,1] 문제 해결 DFS문제 class Solution { public: vector postorder(Node* root) { if(!root){ return {}; } ..
-
[LeetCode] 1971. Find if Path Exists in Graph c++Coding Test/LeetCode 2022. 11. 29. 17:57
문제 There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive). The edges in the graph are represented as a 2D integer array edges, where each edges[i] = [ui, vi] denotes a bi-directional edge between vertex ui and vertex vi. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself. You want to determine if there is a v..
-
[프로그래머스] 숫자 카드 나누기 c++Coding Test/programmers 2022. 11. 25. 15:10
문제 설명 철수와 영희는 선생님으로부터 숫자가 하나씩 적힌 카드들을 절반씩 나눠서 가진 후, 다음 두 조건 중 하나를 만족하는 가장 큰 양의 정수 a의 값을 구하려고 합니다. 철수가 가진 카드들에 적힌 모든 숫자를 나눌 수 있고 영희가 가진 카드들에 적힌 모든 숫자들 중 하나도 나눌 수 없는 양의 정수 a 영희가 가진 카드들에 적힌 모든 숫자를 나눌 수 있고, 철수가 가진 카드들에 적힌 모든 숫자들 중 하나도 나눌 수 없는 양의 정수 a 예를 들어, 카드들에 10, 5, 20, 17이 적혀 있는 경우에 대해 생각해 봅시다. 만약, 철수가 [10, 17]이 적힌 카드를 갖고, 영희가 [5, 20]이 적힌 카드를 갖는다면 두 조건 중 하나를 만족하는 양의 정수 a는 존재하지 않습니다. 하지만, 철수가 [10..