2023/09/06 3

[Leetcode Top Interview 150] 199. Binary Tree Right Side View

난이도 : medium 문제링크 root로부터 오른쪽 측면에서 보았을때 보이는 노드들을 출력하라 1. 접근법 해당 노드 전부를 전위표현식으로 방문하여 리스트에 집어넣음 그리고 오른쪽부터 방문하면서 해당 레벨에서 존재하는 가장 오른쪽 노드를 리스트의 인덱스로 찾아냄 방문하면서 answer 배열에 가장 오른쪽 노드 값을 집어넣음 2. 의사코드 dfs(root); Queue queue = new LinkedList(); queue.offer(root); while (queue.isEmpty()) { // depth 별 answer를 구한다 } public void dfs(TreeNode root) { if (root == null) { return; } list.add(root); dfs(root.right)..

Algorithm/(Java) PS 2023.09.06

[Leetcode Top Interview 150] 230. Kth Smallest Element in a BST

난이도 : medium 문제링크 binary search tree의 root 노드와 int k 값이 주어진다 k번째로 작은 값의 노드를 리턴하라 1. 접근법 중위표현식으로 탐색하면 오름차순으로 탐색이 가능하다 k번째 값을 리턴 2. 의사코드 count = 0; if (count == k) { return root.val; } int ans = Dfs(root.left); if (ans != Integer.MAX_VALUE) { return ans; } count++ if (count == k) { return root.val; } return Dfs(root.right); 3. 구현 코드 class Solution { int count = 0; public int kthSmallest(TreeNode r..

Algorithm/(Java) PS 2023.09.06