From 86561f968e4ca7b42b13a43e99b7a784934c5a66 Mon Sep 17 00:00:00 2001 From: ductnn Date: Mon, 18 Dec 2023 00:45:39 +0700 Subject: [PATCH] add sol --- .../leaf-SimilarTrees.go | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 leetcode/leetcode75/BinaryTree-DFS/872.Leaf-SimilarTrees/leaf-SimilarTrees.go diff --git a/leetcode/leetcode75/BinaryTree-DFS/872.Leaf-SimilarTrees/leaf-SimilarTrees.go b/leetcode/leetcode75/BinaryTree-DFS/872.Leaf-SimilarTrees/leaf-SimilarTrees.go new file mode 100644 index 0000000..cad152d --- /dev/null +++ b/leetcode/leetcode75/BinaryTree-DFS/872.Leaf-SimilarTrees/leaf-SimilarTrees.go @@ -0,0 +1,62 @@ +package main + +import ( + "fmt" +) + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +func leafSimilar(root1 *TreeNode, root2 *TreeNode) bool { + arr1 := []int{} + dfs(root1, &arr1) + + arr2 := []int{} + dfs(root2, &arr2) + + if len(arr1) != len(arr2) { + return false + } + + for i := range arr1 { + if arr1[i] != arr2[i] { + return false + } + } + + return true +} + +func dfs(root *TreeNode, arr *[]int) { + if root == nil { + return + } + if root.Left == nil && root.Right == nil { + *arr = append(*arr, root.Val) + return + } + dfs(root.Left, arr) + dfs(root.Right, arr) +} + +func main() { + // Define the trees + root1 := &TreeNode{Val: 3, Left: &TreeNode{Val: 5, Left: &TreeNode{Val: 6}, Right: &TreeNode{Val: 2, Left: &TreeNode{Val: 7}, Right: &TreeNode{Val: 4}}}, Right: &TreeNode{Val: 1, Left: &TreeNode{Val: 9}, Right: &TreeNode{Val: 8}}} + root2 := &TreeNode{Val: 3, Left: &TreeNode{Val: 5, Left: &TreeNode{Val: 6}, Right: &TreeNode{Val: 7, Left: &TreeNode{Val: 2}, Right: &TreeNode{Val: 4}}}, Right: &TreeNode{Val: 1, Left: &TreeNode{Val: 4}, Right: &TreeNode{Val: 9}}} + + // Check leaf similarity + result := leafSimilar(root1, root2) + fmt.Println(result) +}