[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)..
[LeetCode 75/Stack] 2390 - Removing Stars From a String / 735 - Asteroid Collision / 394 - Decode String (Java)
·
✏️/LeetCode
2390 - Removing Stars From a String풀이*를 만나면 왼쪽에서 가장 가까운 문자 하나 삭제-> 마지막에 들어온 문자(LIFO) 지움 => 스택 일반 문자 -> 스택에 push* -> 스택에서 pop코드class Solution { public String removeStars(String s) { StringBuilder sb = new StringBuilder(); Stack stack = new Stack(); for (char c : s.toCharArray()) { if (c == '*') stack.pop(); else stack.push(c); } for (cha..
aeongg
'분류 전체보기' 카테고리의 글 목록