[LeetCode 75/Graphs - BFS] 1926 - Nearest Exit from Entrance in Maze / 994 - Rotting Oranges
·
✏️/LeetCode
1926 - Nearest Exit from Entrance in Maze풀이2차원, 정점 (r, c), 상하좌우 이동`.`: 이동 가능 / `+`: 벽 (이동 불가)출구: 가장자리에 있는 `.` (!= 시작점)int[] dr = {1, -1, 0, 0};int[] dc = {0, 0, 1, -1}; `q.offer(new int[]{r, c, dist})`: (행, 열, 거리)`maze[nr][nc] = '+'`: 지나간 곳을 벽으로 바꿈코드class Solution { int m, n; int[] dr = {1, -1, 0, 0}; int[] dc = {0, 0, 1, -1}; int bfs(char[][] maze, int[] entrance) { Queue q =..
[LeetCode 75/Binary Search Tree] 700 - Search in a Binary Search Tree / 450 - Delete Node in a BST (Java)
·
✏️/LeetCode
700 - Search in a Binary Search Tree풀이BST에서 값이 val과 같은 노드 찾고 그 노드를 root로 하는 subtreeBST: 왼쪽 subtree node.val > val -> 왼쪽으로 이동node.val 오른쪽으로 이동코드class Solution { public TreeNode searchBST(TreeNode root, int val) { if (root == null) return null; if (root.val == val) return root; if (val 450 - Delete Node in a BST풀이BST에서 값 key 가진 노드 찾아 삭제하고 삭제 후에도 BST 성질 유지 BST: 왼쪽 subtree n..
[LeetCode 75/Binary Tree - BFS] 199 - Binary Tree Right Side View / 1161 - Maximum Level Sum of a Binary Tree (Java)
·
✏️/LeetCode
199 - Binary Tree Right Side View풀이이진 트리의 루트 노드가 주어졌을 때, 트리를 오른쪽에서 바라봄-> 각 높이(level)마다 맨 오른쪽 노드만 보임=> 각 레벨에서 마지막 노드 찾기 BFS로 트리를 레벨 순서로 탐색ex. root = [1, 2, 3, , null, 5, null, 4]level 0 / [1] => 1level 1 / [2, 3] => 3level 2 / [5, 4] => 4=> [1, 3, 4]코드class Solution { public List rightSideView(TreeNode root) { List arr = new ArrayList(); if (root == null) return arr; Queue..
[LeetCode 75/Binary Tree - DFS] 437 - Path Sum 3 / 1372 - Longest ZigZag Path in a Binary Tree / 236 - Lowest Common Ancestor of a Binary Tree (Java)
·
✏️/LeetCode
437 - Path Sum 3풀이부모 -> 자식 경로 중 합이 `targetSum`인 경로 개수=> 모든 노드를 시작점으로 생각 `int dfs(TreeNode node, long target)`: 현재 node에서 시작해서 아래로 내려가며 합이 `target`이 되는 경로 개수`int pathSum(TreeNode root, int targetSum)`: 모든 노드를 시작점으로 고려`dfs(root, targetSum)`: root에서 시작하는 경로 개수`pathSum(root.left, targetSum)`: 왼쪽 subtree에서 시작하는 경로 개수`pathSum(root.right, targetSum)`: 오른쪽 subtree에서 시작하는 경로 개수코드class Solution { int d..
[LeetCode 75/Binary Tree - DFS] 104 - Maximum Depth of Binary Tree / 872 - Leaf-Similar Trees / 1448 - Count Good Nodes in Binary Tree (Java)
·
✏️/LeetCode
104 - Maximum Depth of Binary Tree풀이이진 트리 노드 정의 클래스`val`: 자기 값`left`: 왼쪽 자식`right`: 오른쪽 자식class TreeNode { int val; TreeNode left; TreeNode right; TreeNode() {} TreeNode(int val) { this.val = val; } TreeNode(int val, TreeNode left, TreeNode right) { this.val = val; this.left = left; this.right = right; }} 최대 깊이: 루트에서 가장 먼 리프 노드까지의 가장 긴 경로에 ..
[LeetCode 75/Linked List] 2095 - Delete the Middle Node of a Linked List / 328 - Odd Even Linked List / 206 - Reverse Linked List / 2130 - Maximum Twin Sum of a Linked List (Java)
·
✏️/LeetCode
2095 - Delete the Middle Node of a Linked List풀이길이 n인 리스트에서 인덱스 ⌊n/2⌋ (0-based) 노드 삭제ex. [1, 3, 4, 7, 1, 2, 6] -> n = 7, ⌊7/2⌋ = 3 => 7 삭제 `head.next == null`: 노드 1개 == middle -> 삭제 => null 리스트 길이 모름 -> `fast`: 2칸씩 이동 / `slow`: 1칸씩 이동`fast`가 끝에 도달하면 `slow`는 middle에 있음(`prev`: `slow`의 이전 노드 저장)ListNode slow = head;ListNode fast = head;ListNode prev = null;while (fast != null && fast.next != null)..
aeongg
'✏️' 카테고리의 글 목록