Skip to content

[WEEK06-1] 추슬기#23

Merged
doitchuu merged 6 commits intopnt-fe-study:mainfrom
doitchuu:doitchuu
Mar 18, 2026
Merged

[WEEK06-1] 추슬기#23
doitchuu merged 6 commits intopnt-fe-study:mainfrom
doitchuu:doitchuu

Conversation

@doitchuu
Copy link
Member

이렇게 풀었어요

1. Middle of the Linked List

  • 문제를 풀었어요.
  • 풀이 시간 : 23분

1) 복잡도 계산

  • 시간 복잡도: O(n)
  • 공간 복잡도: O(n)

2) 접근 아이디어

  1. 문자열/배열을 한 번 순회하며 Map 또는 Set에 필요한 상태를 누적했다.
  2. 순회 중 조건을 만족하는 시점에 정답에 필요한 값을 갱신했다.
  3. 누적된 상태를 바탕으로 최종 결과를 계산해 반환했다.

3) 회고

처음에는 index를 직접 세고 Map에 노드를 저장해서 가운데 노드를 찾는 방식으로 풀었다. 구현 자체는 직관적이었는데, 문제를 풀고 나니 굳이 노드를 전부 저장하지 않아도 된다는 점이 아쉽게 느껴졌다. 토끼와 거북이 패턴을 알고는 있었지만, 막상 연결 리스트 문제에 바로 적용하려고 하니 아직은 시간이 더 필요한 것 같다.

다른 사람 풀이:

var middleNode = function(head) {
    let slowPointer = head;
    let fastPointer = head;

    while (fastPointer !== null && fastPointer.next !== null) {
        slowPointer = slowPointer.next;
        fastPointer = fastPointer.next.next;
    }

    return slowPointer;
};

다른 사람 풀이 핵심: 풀이 흐름을 단계별로 분리해 조건 확인과 상태 갱신을 명확히 나눈 점이 핵심이었다.
내 생각: slow, fast 포인터만으로 가운데를 찾는 방식이 훨씬 간결했다. 다음에는 연결 리스트가 나오면 Map으로 저장하기 전에 토끼와 거북이 패턴을 먼저 떠올려보려고 한다.



2. Maximum Depth of Binary Tree

  • 문제를 풀었어요.
  • 풀이 시간 : 24분

1) 복잡도 계산

  • 시간 복잡도: O(1)
  • 공간 복잡도: O(h)

2) 접근 아이디어

  1. 시작 지점에서 인접 위치로 확장하는 탐색 구조를 구성했다.
  2. 방문 조건을 먼저 확인해 중복 방문과 범위 밖 접근을 차단했다.
  3. 탐색이 끝날 때까지 상태를 갱신하고 최종 보드를 반환했다.

3) 회고

DFS 문제가 리트코드처럼 자료 구조가 달라지면 적응하는 게 아직 어렵다. 그래도 depth(node) = max(depth(left), depth(right)) + 1 공식을 기준으로 보니까 조금씩 정리가 됐다. 핵심은 depth가 아래로 전달되는 값이 아니라, 리프에서부터 계산돼서 위로 올라오는 값이라는 점이었다.

다른 사람 풀이:

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxDepth = function(root) {
    const dfs = (node, depth) => {
        if (node === null) {
            res = Math.max(res, depth);
            return;
        }
        dfs(node.left, depth + 1);
        dfs(node.right, depth + 1);
    }

    let res = 0;
    dfs(root, 0);

    return res;
};

다른 사람 풀이 핵심: 탐색 순서를 명확히 제어하면서 방문 조건을 먼저 처리해 예외 케이스를 안정적으로 막은 점이 핵심이었다.
내 생각: depth를 반환값으로 처리하는 방식이 더 익숙했는데, 깊이를 인자로 내려보내며 최대값을 갱신하는 풀이도 가능하다는 점이 흥미로웠다. 같은 DFS라도 상태를 어디에 둘지에 따라 풀이 모양이 꽤 달라진다는 걸 느꼈다.



@doitchuu doitchuu self-assigned this Mar 16, 2026
@doitchuu doitchuu requested review from raejun92 and sik9252 March 16, 2026 03:21
Copy link
Collaborator

@raejun92 raejun92 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

항상 풀이가 완벽하군요!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DFS 재귀로 최대 깊이를 구하는 정석 풀이로 깔끔하게 잘 작성하신 것 같습니다. null 처리와 Math.max(left, right) + 1 흐름도 명확해서 읽기 좋았어요.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 이렇게 Map으로 인덱스와 노드를 저장해서 middle을 찾는 방식으로도 구현할 수 있군요 알아갑니다!

@doitchuu doitchuu merged commit 8dc95c4 into pnt-fe-study:main Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants