diff options
author | terminaldweller <thabogre@gmail.com> | 2022-12-08 05:36:16 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2022-12-08 05:36:16 +0000 |
commit | 7108d6d7be57717b2defbaf32c01dd39d3287e07 (patch) | |
tree | 6b4aa18036c6106b21f78bb723458459d8e04a36 | |
parent | 938 (diff) | |
download | leetcode-7108d6d7be57717b2defbaf32c01dd39d3287e07.tar.gz leetcode-7108d6d7be57717b2defbaf32c01dd39d3287e07.zip |
872
-rw-r--r-- | 938/872/go.mod | 3 | ||||
-rw-r--r-- | 938/872/main.go | 74 |
2 files changed, 77 insertions, 0 deletions
diff --git a/938/872/go.mod b/938/872/go.mod new file mode 100644 index 0000000..10fbec9 --- /dev/null +++ b/938/872/go.mod @@ -0,0 +1,3 @@ +module 872 + +go 1.18 diff --git a/938/872/main.go b/938/872/main.go new file mode 100644 index 0000000..24ef0ab --- /dev/null +++ b/938/872/main.go @@ -0,0 +1,74 @@ +package main + +import "fmt" + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +func preorderVisit(node *TreeNode, leafSequence *[]int) { + if node == nil { + return + } + if node.Left == nil && node.Right == nil { + *leafSequence = append(*leafSequence, node.Val) + return + } + + preorderVisit(node.Left, leafSequence) + preorderVisit(node.Right, leafSequence) +} + +func leafSimilar(root1, root2 *TreeNode) bool { + var leafSequence1 []int + var leafSequence2 []int + + preorderVisit(root1, &leafSequence1) + preorderVisit(root2, &leafSequence2) + + // for i := 0; i < len(leafSequence1); i++ { + // fmt.Println((leafSequence1)[i]) + // } + // for i := 0; i < len(leafSequence2); i++ { + // fmt.Println((leafSequence2)[i]) + // } + + if len(leafSequence1) != len(leafSequence2) { + return false + } + + for i := 0; i < len(leafSequence1); i++ { + if (leafSequence1)[i] != (leafSequence2)[i] { + return false + } + } + + return true +} + +func main() { + var node1 TreeNode + var node2 TreeNode + var node3 TreeNode + node1.Val = 1 + node2.Val = 2 + node3.Val = 3 + + node1.Left = &node2 + node1.Right = &node3 + + var node4 TreeNode + var node5 TreeNode + var node6 TreeNode + node4.Val = 1 + node5.Val = 2 + node6.Val = 3 + + node4.Left = &node6 + node4.Right = &node5 + + fmt.Println(leafSimilar(&node1, &node4)) + +} |