Algorithm/LeetCode
[LeetCode] - Binary Tree Level Order Traversal
Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level). 같은 level의 node의 값을 배열로 묶어서 반환하라 sudo code 저장할 자료구조를 만듬 depth가 늘어날 때 해당 depth와반환형이 List ArrayList nodes = new ArrayList(); ArrayList result = new ArrayList(); depth만큼 들어가면 해당 depth의 node를 반환하는 메서드가 필요 최대 depth만큼 들어갔다는 기준? left, right가 모두 null이 나온다면 최대 depth라고 할 수 있다...
[LeetCode] - Symmetric Tree
이진 트리가 주어지고 중심을 기준으로 거울처럼 대칭인지 확인 sudo code root 기준 left와 right가 같을 경우 true left right 양쪽이 null이면 true left 혹은 right 한쪽이 null이면 false left left와 right의 right가 같아야 하고 left의 right와 right의 left가 같아야 함 1차 시도 class Solution { public boolean isSymmetric(TreeNode root) { if(root == null) return true; return isSame(root.left, root.right); } public boolean isSame(TreeNode left, TreeNode right) { if(left ..
[LeetCode] - Validate Binary Search Tree
Binary Search Tree(BST) 검증하는 문제 root의 left는 root보다 작고 root의 right는 root보다 커야한다. SubTree도 같은 구조를 가져야 한다. /** * 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; * } * } */ c..
[LeetCode] - Maximum Depth of Binary Tree
이진 트리의 최대 깊이를 구하는 문제 의사 코드 root가 null인 경우 depth를 반환 아니면 left 혹은 right를 넣어서 순회 left와 right의 최대 깊이를 구함 둘 중에 큰 수를 반환 1차 시도 /** * 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 = rig..
[LeetCode] - Height Checker
height 배열을 주고 오름차 순으로 정렬한다. 그 다음 원본 height 배열의 요소와 다른 값을 찾아서 누산한 다음에 리턴하는 문제 오늘 문제는 쉬웠다 좋다 :) class Solution { public int heightChecker(int[] heights) { /* 의사 코드 정수 배열인 heights를 오름차순으로 정렬하면서 바뀌는 수를 누산하여 리턴 */ int change = 0; int[] copy = Arrays.copyOf(heights, heights.length); Arrays.sort(heights); for(int idx = 0; idx < heights.length; idx++) { if(heights[idx] != copy[idx]) change++; } return c..
[LeetCode] - Masking Personal Information
pesudo code 1 decide if it is an email or phone 2-1 (email case) Split string array into 3 strings 3-1 Get the first element of a divided string array 4-1 Extracting the first and last characters of an imported string 5-1 Put 5 astarisks in the extracted string and return 6-1 Concatenate the created string with the rest of the string array and return it 2-2 (phone case) Create a phone number by ..