Coding Test/LeetCode
-
[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..
-
[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..