|
| 1 | +# Intuition (DFS & BFS) |
| 2 | +<!-- Describe your first thoughts on how to solve this problem. --> |
| 3 | +We need two main function: one to check the equality of nodes and the subtrees, and another to iterate through the main tree. |
| 4 | + |
| 5 | +We will use both DFS and BFS methods to solve this problem. |
| 6 | + |
| 7 | +Reference. [Same Tree](ttps://leetcode.com/problems/same-tree/solutions/5159658/go-simple-solution) |
| 8 | + |
| 9 | +# Approach |
| 10 | +<!-- Describe your approach to solving the problem. --> |
| 11 | +1. Create a function that, while iterating using DFS(Recursion) or BFS(Queue),checks if the two trees are equal. |
| 12 | +2. First, check if the two trees are equal. If not, iterate through the children of main tree. (`root.Left`, `root.Right`) |
| 13 | +# Complexity (DFS) |
| 14 | +- Time complexity: $$O(n * m)$$ |
| 15 | + (This complexity is $$O(n * m)$$. because the `isSubtree()` iterates throuth all modes while **simultaneously** calling the `isEqualtree()` for each node. |
| 16 | +<!-- Add your time complexity here, e.g. $$O(n)$$ --> |
| 17 | + |
| 18 | +- Space complexity: $$O({h_n} + {h_m})$$ |
| 19 | + (This complexity is determined. because the maximun depth of the call stack, which doesn't exceed the sum of heights of both trees.) |
| 20 | +<!-- Add your space complexity here, e.g. $$O(n)$$ --> |
| 21 | + |
| 22 | +# Code |
| 23 | +``` |
| 24 | +func isEqualTree(root *TreeNode, subRoot *TreeNode) bool { |
| 25 | + if (root == nil) || (subRoot == nil) { |
| 26 | + return root == subRoot |
| 27 | + } |
| 28 | +
|
| 29 | + return (root.Val == subRoot.Val) && isEqualTree(root.Left, subRoot.Left) && isEqualTree(root.Right, subRoot.Right) |
| 30 | +} |
| 31 | +
|
| 32 | +func isSubtree(root *TreeNode, subRoot *TreeNode) bool { |
| 33 | + if root == nil { |
| 34 | + //assert subRoot != nil |
| 35 | + return false |
| 36 | + } |
| 37 | + |
| 38 | + return isEqualTree(root, subRoot) || isSubtree(root.Left, subRoot) || isSubtree(root.Right, subRoot) |
| 39 | +} |
| 40 | +``` |
| 41 | +- - - |
| 42 | +# Complexity (BFS) |
| 43 | +- Time complexity: $$O(n * m)$$ |
| 44 | + (This complexity is $$O(n * m)$$. because the `isSubtree()` iterates throuth all modes while **simultaneously** calling the `isEqualtree()` for each node. |
| 45 | +<!-- Add your time complexity here, e.g. $$O(n)$$ --> |
| 46 | + |
| 47 | +- Space complexity: $$O({h_n} + {h_m})$$ |
| 48 | + (This complexity is determined. because the maximun sizes of the queues (`q`, `q1`, `q2`), which doesn't exceed the sum of sizes of both trees.) |
| 49 | +<!-- Add your space complexity here, e.g. $$O(n)$$ --> |
| 50 | +# Code |
| 51 | +```go |
| 52 | +func isEqualTree(root *TreeNode, subRoot *TreeNode) bool { |
| 53 | + q1 := []*TreeNode{root} |
| 54 | + q2 := []*TreeNode{subRoot} |
| 55 | + |
| 56 | + for len(q1) != 0 { |
| 57 | + f1 := q1[0] |
| 58 | + f2 := q2[0] |
| 59 | + |
| 60 | + q1 = q1[1:] |
| 61 | + q2 = q2[1:] |
| 62 | + |
| 63 | + if (f1 == nil) && (f2 == nil) { |
| 64 | + continue |
| 65 | + } |
| 66 | + if (f1 == nil) || (f2 == nil) || (f1.Val != f2.Val) { |
| 67 | + return false |
| 68 | + } |
| 69 | + |
| 70 | + q1 = append(q1, f1.Left) |
| 71 | + q1 = append(q1, f1.Right) |
| 72 | + |
| 73 | + q2 = append(q2, f2.Left) |
| 74 | + q2 = append(q2, f2.Right) |
| 75 | + } |
| 76 | + |
| 77 | + return true |
| 78 | +} |
| 79 | + |
| 80 | +func isSubtree(root *TreeNode, subRoot *TreeNode) bool { |
| 81 | + if root == nil { |
| 82 | + //assert subRoot != nil |
| 83 | + return false |
| 84 | + } |
| 85 | + |
| 86 | + q := []*TreeNode{root} |
| 87 | + |
| 88 | + for len(q) != 0 { |
| 89 | + node := q[0] |
| 90 | + q = q[1:] |
| 91 | + |
| 92 | + if node == nil { |
| 93 | + continue |
| 94 | + } |
| 95 | + if isEqualTree(node, subRoot) { |
| 96 | + return true |
| 97 | + } |
| 98 | + |
| 99 | + q = append(q, node.Left) |
| 100 | + q = append(q, node.Right) |
| 101 | + } |
| 102 | + |
| 103 | + return false |
| 104 | +} |
| 105 | +``` |
0 commit comments