From e843e692ebf3e575beb2a67de769983eca147200 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 26 Feb 2024 17:01:47 +0100 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB100.=20Same=20Tree=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- go/same-tree.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 go/same-tree.go diff --git a/go/same-tree.go b/go/same-tree.go new file mode 100644 index 0000000..b551458 --- /dev/null +++ b/go/same-tree.go @@ -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) +}