Skip to content

[JooKangsan] 2025.02.06#38

Merged
JooKangsan merged 26 commits intomainfrom
JooKangsan
Feb 13, 2025
Merged

[JooKangsan] 2025.02.06#38
JooKangsan merged 26 commits intomainfrom
JooKangsan

Conversation

@JooKangsan
Copy link
Collaborator

배열

md 파일로 추가했습니다!

📌 푼 문제

문제이름 문제링크
이진 트리의 최대 깊이 https://leetcode.com/problems/maximum-depth-of-binary-tree/
이진 트리의 중위 순회 https://leetcode.com/problems/binary-tree-inorder-traversal/
같은 트리인지 비교 https://leetcode.com/problems/same-tree/description/
이진 트리 반전 https://leetcode.com/problems/invert-binary-tree/
이진 트리 레벨 순서 순회 https://leetcode.com/problems/binary-tree-level-order-traversal/


📝 간단한 풀이 과정

이진 트리의 최대 깊이

function maxDepth(root) {
    if (!root) return 0;

    return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}

이진 트리의 중위 순회

function inorderTraversal(root) {
    const result = [];

    function inorder(node) {
        if (!node) return;

        inorder(node.left);
        result.push(node.val);
        inorder(node.right);
    }

    inorder(root);
    return result;
}

같은 트리인지 비교

function isSameTree(p, q){
    if (!p && !q) return true;
    if (!p || !q || p.val !== q.val) return false;

    return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
  };

이진 트리 반전

function invertTree(root) {
    if (!root) return null;

    [root.left, root.right] = [invertTree(root.right), invertTree(root.left)];

    return root;
    };

이진 트리 레벨 순서 순회

function levelOrder(root){
    if (!root) return [];

    const result = [];
    const queue = [root];

    while (queue.length > 0) {
      const levelSize = queue.length;
      const levelNodes = [];

      for (let i = 0; i < levelSize; i++) {
        const node = queue.shift();
        levelNodes.push(node.val);

        if (node.left) queue.push(node.left);
        if (node.right) queue.push(node.right);
      }

      result.push(levelNodes); 
    }

    return result;
  };

@JooKangsan JooKangsan changed the title Joo kangsan [JooKangsan] 2025.02.06 Feb 6, 2025
@JooKangsan JooKangsan self-assigned this Feb 6, 2025
Copy link
Collaborator

@JustDevRae JustDevRae left a comment

Choose a reason for hiding this comment

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

수고 많으셨습니다! 전체적으로 풀이가 비슷해서 흐름 파악하기 편했습니다!

@JooKangsan JooKangsan merged commit 80aaf9a into main Feb 13, 2025
3 checks passed
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.

2 participants