본문 바로가기

Software Engineering/Algorithm7

[리트코드] 100. Same Tree (tree) 문제 정보 Given the roots of two binary trees p and q, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical, and the nodes have the same value. Example 1: Input: p = [1,2,3], q = [1,2,3] Output: true Example 2: Input: p = [1,2], q = [1,null,2] Output: false 문제 풀이 두 개의 트리를 동시에 순회하면서 다른 부분이 발견될 경우 false를 리턴한다. Writeup /** * .. 2022. 1. 27.
[리트코드] 94. Binary Tree Inorder Traversal (tree, recursion) 문제 정보 Given the root of a binary tree, return the inorder traversal of its nodes' values. Example 1: Input: root = [1,null,2,3] Output: [1,3,2] Example 2: Input: root = [] Output: [] Example 3: Input: root = [1] Output: [1] 문제 풀이 tree의 순회방식을 먼저 이해해야한다. 1 2 3 의 형태일때 1. inorder traversal은 2-1-3 2. preorder traversal은 1-2-3 3. postorder traversal은 1-3-2 문제풀이는 재귀(recursive)를 이용한 방법과 스택을 이용한 방법이 있으나 .. 2022. 1. 27.
[리트코드] 62. Unique Paths (DP) 문제 정보 There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time. Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner. The .. 2022. 1. 27.
[리트코드] 49. Group Anagrams (HashTable) 문제 정보 Given an array of strings strs, group the anagrams together. You can return the answer in any order. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. Example 1: Input: strs = ["eat","tea","tan","ate","nat","bat"] Output: [["bat"],["nat","tan"],["ate","eat","tea"]] Example 2: Input: strs = .. 2022. 1. 26.
[리트코드] 2011. Final Value of Variable After Performing Operations (string) 문제 정보 There is a programming language with only four operations and one variable X: ++X and X++ increments the value of the variable X by 1. --X and X-- decrements the value of the variable X by 1. Initially, the value of X is 0. Given an array of strings operations containing a list of operations, return the final value of X after performing all the operations. 문제 풀이 string 배열에서 string을 하나하나씩 꺼.. 2022. 1. 26.
[리트코드] 1. TwoSum (array) 문제 정보 Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. 문제 풀이 exactly one solution이므로 목표값에서 해당 element를 뺀 나머지 값이 배열에서 존재하는지 찾으면 된다. 동일한 값을 중복으로 더하면 안되므로, 관련 조건을 추가해준다. writeup pu.. 2022. 1. 26.