๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/LeetCode

๐Ÿ’ฏ Daily LeetCode Challenge Day_13 - Same Tree

by HaningYa 2020. 7. 13.
728x90

 


๋ฌธ์ œ

Given two binary trees, 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:     1         1
          / \       / \
         2   3     2   3

        [1,2,3],   [1,2,3]

Output: true

Example 2:

Input:     1         1
          /           \
         2             2

        [1,2],     [1,null,2]

Output: false

Example 3:

Input:     1         1
          / \       / \
         2   1     1   2

        [1,2,1],   [1,1,2]

Output: false

dfs๋กœ tree๋ฅผ ์ˆœํšŒํ•˜๋ฉด ์ˆœํšŒ ์ˆœ์„œ๊ฐ€ ๊ฐ™์•„๋„ ๋‘๊ฐ€์ง€ ๊ฒฝ์šฐ์ผ ๋•Œ tree์˜ ๋ชจ์–‘์ด ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ๋‹ค.

  • ์™ผ์ชฝ leaf ์ธ์ง€ ์˜ค๋ฅธ์ชฝ leaf ์ธ์ง€ (example2)
  • leaf ๋ฐฉํ–ฅ์€ ๊ฐ™์€๋ฐ depth ๊ฐ€ ๋‹ค๋ฅธ์ง€ ([10,5,15] [10,5,null,null,15])

dfs ๋กœ traverse ํ•˜๋ฉด์„œ leaf side ์™€ depth ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ํŠœํ”Œ์„ ๋งŒ๋“ค์–ด ๋ฐฐ์—ด์— ์ €์žฅํ–ˆ๋‹ค.

๋งˆ์ง€๋ง‰์œผ๋กœ ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ํŠœํ”Œ ์š”์†Œ์ค‘ ๋‹ค๋ฅธ๊ฒŒ ์žˆ์œผ๋ฉด return false ํ–ˆ๋‹ค.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public var val: Int
 *     public var left: TreeNode?
 *     public var right: TreeNode?
 *     public init() { self.val = 0; self.left = nil; self.right = nil; }
 *     public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
 *     public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
 *         self.val = val
 *         self.left = left
 *         self.right = right
 *     }
 * }
 */
class Solution {
    var arr1 : [(Int,Int,Int)] = []
    var arr2 : [(Int,Int,Int)] = []
    func isSameTree(_ p: TreeNode?, _ q: TreeNode?) -> Bool {
        if p == nil && q == nil {
            return true
        }else if p == nil && q != nil {
            return false
        }else if p != nil && q == nil {
            return false
        }
        dfs(p!,0,0,0)
        dfs(q!,0,1,0)
        for i in 0..<arr1.count{
            if (arr1[i].0 == arr2[i].0 && arr1[i].1 == arr2[i].1 && arr1[i].2 == arr2[i].2) == false {
                return false
            }
        }
        return true
    }
    func dfs(_ tree : TreeNode, _ side : Int, _ treeKind : Int, _ depth : Int){
        //p: 0 q: 1
        //center: 0, left: 1 right: 2
        if treeKind == 0 {arr1.append((tree.val,side,depth))}
        if treeKind == 1 {arr2.append((tree.val,side,depth))}
        if tree.left != nil {
            dfs(tree.left!,1,treeKind,depth+1)
        }
        if tree.right != nil {
            dfs(tree.right!,2,treeKind,depth+1)
        }
    }
}

 

์ข‹์€ ํ•ด๋‹ต์€ ์•„๋‹Œ๊ฒƒ ๊ฐ™์€๊ฒŒ ๋‘๊ฐœ์˜ tree๋ฅผ ๋™์‹œ์— ํ•˜๋‚˜์”ฉ ์ˆœํšŒํ•˜๋ฉฐ ๋‹ค๋ฅธ ๋ถ€๋ถ„์ด ๋‚˜์˜ค๋Š” ์ฆ‰์‹œ return false ๋ฅผ ํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์€๋ฐ ๋‚ด ์ฝ”๋“œ๋Š” ํŠธ๋ฆฌ์˜ ๊ตฌ์กฐ๊ฐ€ ๊ฐ™๋˜ ๋‹ค๋ฅด๋˜ ๋ฌด์กฐ๊ฑด tree size *2 ๋ฒˆ์„ traverse ํ•ด์•ผ ํ•œ๋‹ค. (tree ์ž์ฒด + ๋ฐฉ๋ฌธ๋ฐฐ์—ด).

์ข€๋” ์ƒ๊ฐํ•ด ๋ด์•ผ๊ฒ ๋‹ค.

728x90

๋Œ“๊ธ€