mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
规范格式
This commit is contained in:
41
leetcode/0872.Leaf-Similar-Trees/872. Leaf-Similar Trees.go
Normal file
41
leetcode/0872.Leaf-Similar-Trees/872. Leaf-Similar Trees.go
Normal file
@ -0,0 +1,41 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
// TreeNode define
|
||||
type TreeNode = structures.TreeNode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
func leafSimilar(root1 *TreeNode, root2 *TreeNode) bool {
|
||||
leaf1, leaf2 := []int{}, []int{}
|
||||
dfsLeaf(root1, &leaf1)
|
||||
dfsLeaf(root2, &leaf2)
|
||||
if len(leaf1) != len(leaf2) {
|
||||
return false
|
||||
}
|
||||
for i := range leaf1 {
|
||||
if leaf1[i] != leaf2[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func dfsLeaf(root *TreeNode, leaf *[]int) {
|
||||
if root != nil {
|
||||
if root.Left == nil && root.Right == nil {
|
||||
*leaf = append(*leaf, root.Val)
|
||||
}
|
||||
dfsLeaf(root.Left, leaf)
|
||||
dfsLeaf(root.Right, leaf)
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question872 struct {
|
||||
para872
|
||||
ans872
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para872 struct {
|
||||
one []int
|
||||
two []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans872 struct {
|
||||
one bool
|
||||
}
|
||||
|
||||
func Test_Problem872(t *testing.T) {
|
||||
|
||||
qs := []question872{
|
||||
|
||||
question872{
|
||||
para872{[]int{-10, -3, 0, 5, 9}, []int{-10, -3, 0, 5, 9}},
|
||||
ans872{true},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 872------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans872, q.para872
|
||||
tree1 := structures.Ints2TreeNode(p.one)
|
||||
tree2 := structures.Ints2TreeNode(p.two)
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, leafSimilar(tree1, tree2))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
61
leetcode/0872.Leaf-Similar-Trees/README.md
Normal file
61
leetcode/0872.Leaf-Similar-Trees/README.md
Normal file
@ -0,0 +1,61 @@
|
||||
# 872. Leaf-Similar Trees
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Consider all the leaves of a binary tree. From left to right order, the values of those leaves form a *leaf value sequence.*
|
||||
|
||||

|
||||
|
||||
For example, in the given tree above, the leaf value sequence is `(6, 7, 4, 9, 8)`.
|
||||
|
||||
Two binary trees are considered *leaf-similar* if their leaf value sequence is the same.
|
||||
|
||||
Return `true` if and only if the two given trees with head nodes `root1` and `root2` are leaf-similar.
|
||||
|
||||
**Note**:
|
||||
|
||||
- Both of the given trees will have between `1` and `100` nodes.
|
||||
|
||||
## 题目大意
|
||||
|
||||
请考虑一颗二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 叶值序列 。举个例子,如上图所示,给定一颗叶值序列为 (6, 7, 4, 9, 8) 的树。如果有两颗二叉树的叶值序列是相同,那么我们就认为它们是 叶相似 的。如果给定的两个头结点分别为 root1 和 root2 的树是叶相似的,则返回 true;否则返回 false 。
|
||||
|
||||
提示:
|
||||
|
||||
- 给定的两颗树可能会有 1 到 200 个结点。
|
||||
- 给定的两颗树上的值介于 0 到 200 之间。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给出 2 棵树,如果 2 棵树的叶子节点组成的数组是完全一样的,那么就认为这 2 棵树是“叶子相似”的。给出任何 2 棵树判断这 2 棵树是否是“叶子相似”的。
|
||||
- 简单题,分别 DFS 遍历 2 棵树,把叶子节点都遍历出来,然后分别比较叶子节点组成的数组是否完全一致即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
func leafSimilar(root1 *TreeNode, root2 *TreeNode) bool {
|
||||
leaf1, leaf2 := []int{}, []int{}
|
||||
dfsLeaf(root1, &leaf1)
|
||||
dfsLeaf(root2, &leaf2)
|
||||
if len(leaf1) != len(leaf2) {
|
||||
return false
|
||||
}
|
||||
for i := range leaf1 {
|
||||
if leaf1[i] != leaf2[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func dfsLeaf(root *TreeNode, leaf *[]int) {
|
||||
if root != nil {
|
||||
if root.Left == nil && root.Right == nil {
|
||||
*leaf = append(*leaf, root.Val)
|
||||
}
|
||||
dfsLeaf(root.Left, leaf)
|
||||
dfsLeaf(root.Right, leaf)
|
||||
}
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user