Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d956750

Browse files
authored
Merge pull request neetcode-gh#764 from MaratKhakim/572-Subtree-Of-Another-Tree-Go
Go: 572. Subtree of Another Tree
2 parents 6aecc8c + f892702 commit d956750

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

go/572-Subtree-of-Another-Tree.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func isSubtree(root *TreeNode, subRoot *TreeNode) bool {
10+
if root == nil {
11+
return false
12+
}
13+
14+
return equals(root, subRoot) || isSubtree(root.Left, subRoot) || isSubtree(root.Right, subRoot)
15+
}
16+
17+
func equals(s *TreeNode, t *TreeNode) bool {
18+
if s == nil && t == nil {
19+
return true
20+
}
21+
22+
if s == nil || t == nil || s.Val != t.Val {
23+
return false
24+
}
25+
26+
return equals(s.Left, t.Left) && equals(s.Right, t.Right)
27+
}

0 commit comments

Comments
 (0)