100. ๊ฐ™์€ ๋‚˜๋ฌด ๐Ÿš€

5625 ๋‹จ์–ด

์†”๋ฃจ์…˜ ๊ฐœ๋ฐœ:



JavaScript

์งˆ๋ฌธ



์ด ๊ธฐ์‚ฌ์—์„œ๋Š” Leetcode์˜ '100. Same 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.



์˜ˆ์‹œ:

     1      1
    / \    / \
   2   3  2   3




Input: p = [1,2,3], q = [1,2,3]
Output: true


์งˆ๋ฌธ ์„ค๋ช…



์šฐ๋ฆฌ๊ฐ€ ์š”๊ตฌํ•˜๋Š” ๊ฒƒ์€ ๋‘ ์ด์ง„ ํŠธ๋ฆฌ๊ฐ€ ๋™์ผํ•œ์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‘ ํŠธ๋ฆฌ์—์„œ nodes์˜ ๋ชจ๋“  ๊ฐ’์„ ๋™์ผํ•œ ์ˆœ์„œ๋กœ ๋น„๊ตํ•  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋ฉ๋‹ˆ๋‹ค.

๊ถŒ์žฅ ์ง€์‹


  • Binary Tree
  • Depth First Search (Recursive)
  • Javascript Stack
  • Recursion

  • ์šฐ๋ฆฌ๋Š” ๋ฌด์—‡์„ ์•Œ๊ณ  ์žˆ์Šต๋‹ˆ๊นŒ?


  • 2๊ฐœ์˜ ํŠธ๋ฆฌ๊ฐ€ ์žˆ๊ณ  ๋™์ผํ•œ์ง€ ํ™•์ธํ•ด์•ผ ํ•จ
  • ํŠธ๋ฆฌ๊ฐ€ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๋…ธ๋“œ ์ˆ˜๋Š” 100์ž…๋‹ˆ๋‹ค.

  • ์–ด๋–ป๊ฒŒ ํ•  ๊ฒƒ์ธ๊ฐ€:


  • ์ด ์ž‘์—…์„ ์žฌ๊ท€์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๋‘ ํŠธ๋ฆฌ์˜ ๊ฐ ๋…ธ๋“œ์—์„œ ์šฐ๋ฆฌ๋Š” ํ•œ ์ง€์ ์„ ๋ฐฉ๋ฌธํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. left ๋˜๋Š” right ํฌ์ธํ„ฐ
  • ๋กœ isSameTree ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ด๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  • ๋™์‹œ์— ๋‘ ํŠธ๋ฆฌ์˜ ๊ฐ ๋…ธ๋“œ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ํŠธ๋ฆฌ 1์—์„œ ์™ผ์ชฝ์œผ๋กœ ์ด๋™ํ•˜๋ฉด ํŠธ๋ฆฌ 2์—์„œ๋„ ์™ผ์ชฝ์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. ์˜ค๋ฅธ์ชฝ ํŠธ๋ฆฌ์˜ ๋งจ ๋์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๋˜ํ•œ ์˜ค๋ฅธ์ชฝ ํŠธ๋ฆฌ์—์„œ๋„ ์ด๊ฒƒ์„ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  • ์ƒˆ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•  ๋•Œ๋งˆ๋‹ค ๋‘ ๋…ธ๋“œ๊ฐ€ ๋น„์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ๋‘ ํŠธ๋ฆฌ ๋ชจ๋‘ ๋น„์–ด ์žˆ๊ณ  ๋์— ์žˆ์œผ๋ฏ€๋กœ ์ •ํ™•ํ•ฉ๋‹ˆ๋‹ค.
  • ์žˆ์–ด์•ผ ํ•  ํฌ์ธํ„ฐnull๊ฐ€ ์žˆ์Šต๋‹ˆ๊นŒ? ๋ฏธ๋Ÿฌ ๋…ธ๋“œ๊ฐ€ null์ด ์•„๋‹˜์„ ์˜๋ฏธํ•ฉ๋‹ˆ๊นŒ? ์ด ๊ฒฝ์šฐ ์ž˜๋ชป๋œ ํŠธ๋ฆฌ์ž…๋‹ˆ๋‹ค.
  • ๊ทธ๋Ÿฐ ๋‹ค์Œ ๊ฐ’์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋“ค์€ ๋™์ผํ•˜์ง€ ์•Š์Šต๋‹ˆ๊นŒ? ๊ทธ๋Ÿฌ๋ฉด ์ž˜๋ชป๋œ ํŠธ๋ฆฌ์ž…๋‹ˆ๋‹ค
  • .
  • ์™ผ์ชฝ ํŠธ๋ฆฌ์™€ ์˜ค๋ฅธ์ชฝ ํŠธ๋ฆฌ๊ฐ€ ๋ชจ๋‘ ์œ ํšจํ•œ์ง€ ๋น„๊ตํ•˜๋ฉด์„œ ๋ชจ๋“  ๋…ธ๋“œ๊ฐ€ ์†Œ์ง„๋  ๋•Œ๊นŒ์ง€ ์ด ์ž‘์—…์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

  • ๋น…์˜ค ํ‘œ๊ธฐ๋ฒ•:


  • ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n) | ์—ฌ๊ธฐ์„œ n์€ ๋‘ ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ ์ˆ˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. | ์ตœ์•…์˜ ๊ฒฝ์šฐ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•ฉ๋‹ˆ๋‹ค
  • .
  • ๊ณต๊ฐ„ ๋ณต์žก๋„: O(h) | ์—ฌ๊ธฐ์„œ h๋Š” ๊ฐ€์žฅ ๋†’์€ ๋‚˜๋ฌด์˜ ๋†’์ด์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ํ˜ธ์ถœ ์Šคํƒ | ์ตœ์•…์˜ ๊ฒฝ์šฐ ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ ์ˆ˜๋Š” ๋†’์ด์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์‹ค์ œ ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(n)์ด๋ผ๊ณ  ์ฃผ์žฅํ•ฉ๋‹ˆ๋‹ค
  • .

    ๋ฆฌํŠธ์ฝ”๋“œ ๊ฒฐ๊ณผ:



    ์ œ์ถœ ๋งํฌ ์ฐธ์กฐ:
  • ๋Ÿฐํƒ€์ž„: 64ms, Same Tree์— ๋Œ€ํ•œ JavaScript ์˜จ๋ผ์ธ ์ œ์ถœ์˜ 84.15%๋ณด๋‹ค ๋น ๋ฆ„.
  • ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰: 42.3MB, Same Tree์— ๋Œ€ํ•œ JavaScript ์˜จ๋ผ์ธ ์ œ์ถœ๋ฌผ์˜ 58.35% ๋ฏธ๋งŒ.




  • ํ•ด๊ฒฐ์ฑ…




    /**
     * 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} p
     * @param {TreeNode} q
     * @return {boolean}
     */
    var isSameTree = function (p, q) {
        /* -------------------------------------------------------------------------- */
        /*                               100. Same Tree                               */
        /* -------------------------------------------------------------------------- */
    
        /**
         * @author  Samuel Hinchliffe
         * @see    {@link linkedin.com/in/samuel-hinchliffe-๐Ÿš€-2bb5801a5/ | Author's Linkedin }
         * @see    {@link github.com/Samuel-Hinchliffe}
         */
    
    
        /* ----------------------------- Solution Below ----------------------------- */
    
        // So both our trees current node is null
        // This mean's they both reached the end of the tree
        // at the same time without error
        if (p == null && q == null) {
            return true;
        }
    
        // One of the pointers are null when another is not
        // This mean's one of our pointers has traversed to a correct node
        // but another has reached the end of the list too early and thus
        // cannot be a valid mirror tree
        if ((p == null && q != null) || (q == null && p != null)) {
            return false;
        }
    
        // As we have moved nodes
        // Are they the same value?
        if (p.val != q.val) {
            return false;
        }
    
        // Get both left nodes
        // We will traverse the left nodes in a DFS fashion
        // to be able to compare both left nodes at the same time
        // So we move left at the same time on both trees.
        let good_lefts = isSameTree(p.left, q.left);
    
        // Get both right nodes
        // We will traverse the right nodes in a DFS fashion
        // to be able to compare both right nodes at the same time
        // So we move right at the same time on both trees.
        let good_rights = isSameTree(p.right, q.right);
    
        // So are both sides good?
        return good_lefts && good_rights;
    };
    
    

    ์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ