본문 바로가기

Algorithm/DFS

(43)
(C++) - LeetCode (easy) 1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree https://leetcode.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/description/ Find a Corresponding Node of a Binary Tree in a Clone of That Tree - LeetCode Can you solve this real interview question? Find a Corresponding Node of a Binary Tree in a Clone of That Tree - Given two binary trees original and cloned and given a reference to a node target in the original..
(C++) - LeetCode (easy) 993. Cousins in Binary Tree https://leetcode.com/problems/cousins-in-binary-tree/description/ LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 자료구조 순회 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 구조체 Info를 선언합니다. node의 깊이 depth와 부모정보 parent를 member 변수로 선언했습니다.x와 y의 정보를 저..
(C++) - LeetCode (easy) 965. Univalued Binary Tree https://leetcode.com/problems/univalued-binary-tree/description/ LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com tree 순회 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 tree node의 종류를 저장할 set s를 선언해줍니다. 📔 풀이과정 preOrder로 s에 각 tree node를 순회한 결과를 저..
(C++) - LeetCode (easy) 938. Range Sum of BST https://leetcode.com/problems/range-sum-of-bst/description/ Range Sum of BST - LeetCode Can you solve this real interview question? Range Sum of BST - Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high]. Example 1: [https://assets.l leetcode.com 재귀 구현 문제였습니다. 📕 풀이방법 📔 풀이과정 왼쪽 sub tree 의 정..
(C++) - LeetCode (easy) 700. Search in a Binary Search Tree https://leetcode.com/problems/search-in-a-binary-search-tree/description/ Search in a Binary Search Tree - LeetCode Can you solve this real interview question? Search in a Binary Search Tree - You are given the root of a binary search tree (BST) and an integer val. Find the node in the BST that the node's value equals val and return the subtree rooted with that node. If leetcode.com 재귀를 통해 답을 찾는..
(C++) - LeetCode (easy) 404. Sum of Left Leaves https://leetcode.com/problems/sum-of-left-leaves/description/ Sum of Left Leaves - LeetCode Can you solve this real interview question? Sum of Left Leaves - Given the root of a binary tree, return the sum of all left leaves. A leaf is a node with no children. A left leaf is a leaf that is the left child of another node. Example 1: [https://ass leetcode.com 재귀를 이용한 자료구조 문제였습니다. 📕 풀이방법 📔 풀이과정 현재 n..
(C++) - LeetCode (easy) 104. Maximum Depth of Binary Tree https://leetcode.com/problems/maximum-depth-of-binary-tree/ Maximum Depth of Binary Tree - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com dfs로 해결한 문제였습니다. 📕 풀이방법 📔 풀이과정 terminal node에서 0을 반환하고 더 깊은 depth로부터 1씩 더해 가장 큰 depth를 반환하는 dfs함수를 구현합니다. 📔 정답출력 dfs함수의 결과를 반환합니다. 📕 Code 📔 C++..
(C++) - 백준(BOJ) 16929 : Two dots https://www.acmicpc.net/problem/16929 16929번: Two Dots 첫째 줄에 게임판의 크기 N, M이 주어진다. 둘째 줄부터 N개의 줄에 게임판의 상태가 주어진다. 게임판은 모두 점으로 가득차 있고, 게임판의 상태는 점의 색을 의미한다. 점의 색은 알파벳 대문 www.acmicpc.net cycle을 판별하는 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 행 n, 열 m, 정답 ans, 시작점 sr,sc, 방문여부를 확인할 ck, 방향 dr,dc, board 를 선언하고 입력받습니다. 📔 풀이과정 2중 for loop를 수행하면서 같은 문자끼리 dfs로 방문해줍니다. 1. 인접칸을 방문하면서 dfs함수를 호출해줍니다. 호출당 길이가 1씩 증가합니다. 2. 최소 사이클의 길..