leetcode
-
[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..
-
[Github] Leetcode 연동Github 2022. 12. 3. 19:21
1. 구글 확장 프로그램 Leethub 설치 LeetHub Automatically integrate your Leetcode & GeeksforGeeks submissions to GitHub chrome.google.com 2. GitHub 계정 연동 3. 옵션 선택 - 기존 repository에 연결 - 새로운 private repository에 연결 중 원하는 방법으로 선택합니다. 저는 새로운 private repository에 연결했습니다. 4. 연동이 완료되면 이렇게 문제 풀이 통계도 나옵니다. 5. leetcode에서 문제를 풀면 자동으로 github에 올라갑니다. 저는 private을 public으로 변경하였습니다. public ↔ private 변경 방법은 settings > Danger..
-
[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..