go: add «100. Same Tree»

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2024-02-26 17:01:47 +01:00
parent ffef9f6188
commit e843e692eb
Signed by: mfocko
GPG key ID: 7C47D46246790496

23
go/same-tree.go Normal file
View file

@ -0,0 +1,23 @@
package same_tree
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isSameTree(p *TreeNode, q *TreeNode) bool {
// at least one is nil
if p == nil || q == nil {
return p == q
}
// different values
if p.Val != q.Val {
return false
}
return isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)
}