Last active
July 1, 2020 20:03
-
-
Save alldroll/13405b770e4a0ffee33138bc3827c173 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Definition for a binary tree node. | |
| * type TreeNode struct { | |
| * Val int | |
| * Left *TreeNode | |
| * Right *TreeNode | |
| * } | |
| */ | |
| func isSubtree(s *TreeNode, t *TreeNode) bool { | |
| queue := []*TreeNode{s} | |
| for len(queue) > 0 { | |
| node := queue[0] | |
| queue = queue[1:] | |
| if isEquals(node, t) { | |
| return true | |
| } | |
| if node.Left != nil { | |
| queue = append(queue, node.Left) | |
| } | |
| if node.Right != nil { | |
| queue = append(queue, node.Right) | |
| } | |
| } | |
| return false | |
| } | |
| func isEquals(s, t *TreeNode) bool { | |
| if s == t { | |
| return true | |
| } | |
| if s == nil || t == nil { | |
| return false | |
| } | |
| return s.Val == t.Val && isEquals(s.Left, t.Left) && isEquals(s.Right, t.Right) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment