add new solutions
This commit is contained in:
parent
9a10695e8c
commit
ca24d0a56a
30 changed files with 697 additions and 16 deletions
34
solutions/8/q872/solution.go
Normal file
34
solutions/8/q872/solution.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package q872
|
||||
|
||||
import "slices"
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
func leafSeq(node *TreeNode, cache []int) []int {
|
||||
if cache == nil {
|
||||
cache = []int{}
|
||||
}
|
||||
leaf := true
|
||||
if node.Left != nil {
|
||||
leaf = false
|
||||
cache = leafSeq(node.Left, cache)
|
||||
}
|
||||
if node.Right != nil {
|
||||
leaf = false
|
||||
cache = leafSeq(node.Right, cache)
|
||||
}
|
||||
if leaf {
|
||||
cache = append(cache, node.Val)
|
||||
}
|
||||
return cache
|
||||
}
|
||||
|
||||
func leafSimilar(root1 *TreeNode, root2 *TreeNode) bool {
|
||||
return slices.Equal(leafSeq(root1, nil), leafSeq(root2, nil))
|
||||
}
|
||||
|
||||
var _ = leafSimilar
|
||||
Loading…
Add table
Add a link
Reference in a new issue