go: add «100. Same Tree»
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
ffef9f6188
commit
e843e692eb
1 changed files with 23 additions and 0 deletions
23
go/same-tree.go
Normal file
23
go/same-tree.go
Normal 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)
|
||||||
|
}
|
Loading…
Reference in a new issue