|
| 1 | +# Intuition (DFS, Recursion) |
| 2 | +<!-- Describe your first thoughts on how to solve this problem. --> |
| 3 | +Recursion is natural method to iterate trees. (Particularly, multiple trees!) |
| 4 | +# Approach |
| 5 | +<!-- Describe your approach to solving the problem. --> |
| 6 | +1. Child nodes(i.e. Left and Right) are compared eqaulity with their subtrees. |
| 7 | +2. Parent nodes check their own values (`Val`) and their children's comparisions. |
| 8 | + |
| 9 | +(Tip: Comparing the values of nodes before recursion is more efficient. due to **short circuit**, which stops further evaluation(`isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)`) when the outcome is already determined by comparing `p.Val == q.Val`) |
| 10 | +# Complexity |
| 11 | +- Time complexity: $$O(n+m)$$ |
| 12 | +<!-- Add your time complexity here, e.g. $$O(n)$$ --> |
| 13 | + |
| 14 | +- Space complexity: $$O(h_n + h_m)$$ |
| 15 | +<!-- Add your space complexity here, e.g. $$O(n)$$ --> |
| 16 | + |
| 17 | +(n and m are number of nodes in trees p and q. $$h_n$$ and $$h_m$$ are their heights.) |
| 18 | +# Code |
| 19 | +``` |
| 20 | +func isSameTree(p *TreeNode, q *TreeNode) bool { |
| 21 | + if p == nil || q == nil { |
| 22 | + return p == nil && q == nil |
| 23 | + } |
| 24 | +
|
| 25 | + return p.Val == q.Val && isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right) |
| 26 | +} |
| 27 | +``` |
| 28 | +- - - |
| 29 | +# BFS |
| 30 | +# Approach |
| 31 | +<!-- Describe your approach to solving the problem. --> |
| 32 | +1. Like a typical BFS solution, Create Queue and iterate through the tree. However, in this case, mulitple queues are required. |
| 33 | +2. While Iterating, Check equality two nodes in p and q. |
| 34 | +# Complexity |
| 35 | +- Time complexity: $$O(n+m)$$ |
| 36 | +<!-- Add your time complexity here, e.g. $$O(n)$$ --> |
| 37 | + |
| 38 | +- Space complexity: $$O(n + m)$$ |
| 39 | +<!-- Add your space complexity here, e.g. $$O(n)$$ --> |
| 40 | + |
| 41 | +(n and m are number of nodes in trees p and q.) |
| 42 | +# Code |
| 43 | +``` |
| 44 | +func updateQueue(node *TreeNode, queue []*TreeNode) []*TreeNode { |
| 45 | + queue = append(queue, node.Left) |
| 46 | + queue = append(queue, node.Right) |
| 47 | +
|
| 48 | + return queue |
| 49 | +} |
| 50 | +
|
| 51 | +func isSameTree(p *TreeNode, q *TreeNode) bool { |
| 52 | + if p == nil || q == nil { |
| 53 | + return p == nil && q == nil |
| 54 | + } |
| 55 | + pQueue := []*TreeNode{p} |
| 56 | + qQueue := []*TreeNode{q} |
| 57 | +
|
| 58 | + for len(pQueue) != 0 { |
| 59 | + pCurr := pQueue[0] |
| 60 | + qCurr := qQueue[0] |
| 61 | +
|
| 62 | + pQueue = pQueue[1:] |
| 63 | + qQueue = qQueue[1:] |
| 64 | +
|
| 65 | + if pCurr == nil && qCurr == nil { |
| 66 | + continue |
| 67 | + } |
| 68 | +
|
| 69 | + if (pCurr == nil || qCurr == nil) || (pCurr.Val != qCurr.Val) { |
| 70 | + return false |
| 71 | + } |
| 72 | + pQueue = updateQueue(pCurr, pQueue) |
| 73 | + qQueue = updateQueue(qCurr, qQueue) |
| 74 | + } |
| 75 | +
|
| 76 | + return true |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +# What I learned |
| 81 | +- Short circuit In Go. |
| 82 | +- Function couldn't update original value (like `updateQueue()'s queue`) |
0 commit comments