diff --git a/binary-tree-level-order-traversal/chjung99.java b/binary-tree-level-order-traversal/chjung99.java new file mode 100644 index 0000000000..a2264ecd4a --- /dev/null +++ b/binary-tree-level-order-traversal/chjung99.java @@ -0,0 +1,51 @@ +/** + * Definition for a binary tree node. + * public 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; + * } + * } + */ +class Solution { + public List> levelOrder(TreeNode root) { + List> answer = new ArrayList<>(); + Deque queue = new ArrayDeque<>(); + int[] visit = new int[2000]; + if (root != null){ + queue.add(new Pair(root, 0)); + } + + while (!queue.isEmpty()) { + Pair cur = queue.poll(); + // System.out.println(cur.node.val+ " depth="+cur.depth); + if (visit[cur.depth] == 0){ + answer.add(new ArrayList<>()); + visit[cur.depth] = 1; + } + List ls = answer.get(cur.depth); + ls.add(cur.node.val); + + if (cur.node.left != null) queue.add(new Pair(cur.node.left, cur.depth + 1)); + if (cur.node.right != null) queue.add(new Pair(cur.node.right, cur.depth + 1)); + } + return answer; + } + class Pair{ + TreeNode node; + int depth; + + public Pair(TreeNode node, int depth){ + this.node = node; + this.depth = depth; + } + } +} + + diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/chjung99.java b/construct-binary-tree-from-preorder-and-inorder-traversal/chjung99.java new file mode 100644 index 0000000000..7b1d355843 --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/chjung99.java @@ -0,0 +1,47 @@ +import java.util.HashMap; +import java.util.Map; + +class Solution { + // 빠른 조회를 위해 inorder의 값과 인덱스를 저장할 Map + private Map inMap; + + public TreeNode buildTree(int[] preorder, int[] inorder) { + inMap = new HashMap<>(); + for (int i = 0; i < inorder.length; i++) { + inMap.put(inorder[i], i); + } + + return construct(preorder, 0, preorder.length - 1, 0, inorder.length - 1); + } + + private TreeNode construct(int[] preorder, int preStart, int preEnd, int inStart, int inEnd) { + // 기저 조건: 더 이상 처리할 노드가 없는 경우 + if (preStart > preEnd || inStart > inEnd) { + return null; + } + + // 1. preorder의 현재 구간 첫 번째 원소가 루트 노드입니다. + int rootVal = preorder[preStart]; + TreeNode root = new TreeNode(rootVal); + + // 2. inorder에서 루트의 위치를 찾아 왼쪽/오른쪽 서브트리 범위를 나눕니다. + int rootIdx = inMap.get(rootVal); + int leftSize = rootIdx - inStart; // 왼쪽 서브트리에 포함된 노드 개수 + + // 3. 재귀적으로 왼쪽 자식 노드들을 연결합니다. + // preorder 범위: 루트 다음(preStart + 1)부터 개수(leftSize)만큼 + // inorder 범위: 원래 시작점부터 루트 앞(rootIdx - 1)까지 + root.left = construct(preorder, preStart + 1, preStart + leftSize, + inStart, rootIdx - 1); + + // 4. 재귀적으로 오른쪽 자식 노드들을 연결합니다. + // preorder 범위: 왼쪽 식구들 끝난 지점 다음(preStart + leftSize + 1)부터 끝까지 + // inorder 범위: 루트 다음(rootIdx + 1)부터 끝까지 + root.right = construct(preorder, preStart + leftSize + 1, preEnd, + rootIdx + 1, inEnd); + + return root; + } +} + + diff --git a/longest-consecutive-sequence/chjung99.java b/longest-consecutive-sequence/chjung99.java new file mode 100644 index 0000000000..db0104db9b --- /dev/null +++ b/longest-consecutive-sequence/chjung99.java @@ -0,0 +1,64 @@ +class Solution { + int[] dp; + int[] next; + int n; + Map location; + + public int longestConsecutive(int[] nums) { + n = nums.length; + dp = new int[n]; + next = new int[n]; + location = new HashMap<>(); + + for (int i = 0; i < n; i++){ + location.put(nums[i], i); + dp[i] = -1; + } + + for (int i = 0; i < n; i++){ + int nextValue = nums[i] + 1; + if (location.containsKey(nextValue)) { + next[i] = location.get(nextValue); + continue; + } + next[i] = -1; + } + + for (int i = 0; i < n; i++) { + if (next[i] == -1) { + dp[i] = 1; + continue; + } + if (dp[next[i]] == -1) { + dp[i] = 1 + find(next[i], nums); + continue; + } + if (dp[next[i]] != -1){ + dp[i] = 1 + dp[next[i]]; + } + } + int answer = 0; + for (int i = 0; i < n; i++){ + // System.out.print(dp[i] + " "); + answer = Math.max(answer, dp[i]); + } + return answer; + } + + public int find(int idx, int[] nums) { + if (next[idx] == -1) { + dp[idx] = 1; + } + else if (dp[next[idx]] == -1) { + dp[idx] = 1 + find(next[idx], nums); + } + else if (dp[next[idx]] != -1) { + dp[idx] = 1 + dp[next[idx]]; + } + return dp[idx]; + } + + +} + + diff --git a/validate-binary-search-tree/chjung99.java b/validate-binary-search-tree/chjung99.java new file mode 100644 index 0000000000..ad570f2f8e --- /dev/null +++ b/validate-binary-search-tree/chjung99.java @@ -0,0 +1,59 @@ +/** + * Definition for a binary tree node. + * public 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; + * } + * } + */ +class Solution { + boolean answer = true; + Deque leftStack = new ArrayDeque<>(); + Deque rightStack = new ArrayDeque<>(); + + public boolean isValidBST(TreeNode root) { + rightStack.push(root.val); + dfs(root.left, root, "left"); + rightStack.pop(); + + leftStack.push(root.val); + dfs(root.right, root, "right"); + leftStack.pop(); + + return answer; + } + + public void dfs(TreeNode node, TreeNode parent, String direction) { + if (node == null) return; + + if (direction.equals("left") && node.val >= parent.val){ + answer = false; + } + if (direction.equals("right") && node.val <= parent.val){ + answer = false; + } + if (!leftStack.isEmpty() && node.val <= leftStack.peek()) { + answer = false; + } + if (!rightStack.isEmpty() && node.val >= rightStack.peek()) { + answer = false; + } + + rightStack.push(node.val); + dfs(node.left, node, "left"); + rightStack.pop(); + + leftStack.push(node.val); + dfs(node.right, node, "right"); + leftStack.pop(); + } +} + +