mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
规范格式
This commit is contained in:
30
leetcode/0100.Same-Tree/100. Same Tree.go
Normal file
30
leetcode/0100.Same-Tree/100. Same Tree.go
Normal file
@ -0,0 +1,30 @@
|
||||
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 isSameTree(p *TreeNode, q *TreeNode) bool {
|
||||
if p == nil && q == nil {
|
||||
return true
|
||||
} else if p != nil && q != nil {
|
||||
if p.Val != q.Val {
|
||||
return false
|
||||
}
|
||||
return isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
73
leetcode/0100.Same-Tree/100. Same Tree_test.go
Normal file
73
leetcode/0100.Same-Tree/100. Same Tree_test.go
Normal file
@ -0,0 +1,73 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question100 struct {
|
||||
para100
|
||||
ans100
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para100 struct {
|
||||
one []int
|
||||
two []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans100 struct {
|
||||
one bool
|
||||
}
|
||||
|
||||
func Test_Problem100(t *testing.T) {
|
||||
|
||||
qs := []question100{
|
||||
|
||||
question100{
|
||||
para100{[]int{}, []int{}},
|
||||
ans100{true},
|
||||
},
|
||||
|
||||
question100{
|
||||
para100{[]int{}, []int{1}},
|
||||
ans100{false},
|
||||
},
|
||||
|
||||
question100{
|
||||
para100{[]int{1}, []int{1}},
|
||||
ans100{true},
|
||||
},
|
||||
|
||||
question100{
|
||||
para100{[]int{1, 2, 3}, []int{1, 2, 3}},
|
||||
ans100{true},
|
||||
},
|
||||
|
||||
question100{
|
||||
para100{[]int{1, 2}, []int{1, structures.NULL, 2}},
|
||||
ans100{false},
|
||||
},
|
||||
|
||||
question100{
|
||||
para100{[]int{1, 2, 1}, []int{1, 1, 2}},
|
||||
ans100{false},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 100------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans100, q.para100
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
rootOne := structures.Ints2TreeNode(p.one)
|
||||
rootTwo := structures.Ints2TreeNode(p.two)
|
||||
fmt.Printf("【output】:%v \n", isSameTree(rootOne, rootTwo))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
56
leetcode/0100.Same-Tree/README.md
Normal file
56
leetcode/0100.Same-Tree/README.md
Normal file
@ -0,0 +1,56 @@
|
||||
# [100. Same Tree](https://leetcode.com/problems/same-tree/)
|
||||
|
||||
## 题目
|
||||
|
||||
|
||||
Given two binary trees, write a function to check if they are the same or not.
|
||||
|
||||
Two binary trees are considered the same if they are structurally identical and the nodes have the same value.
|
||||
|
||||
Example 1:
|
||||
|
||||
|
||||
```c
|
||||
Input: 1 1
|
||||
/ \ / \
|
||||
2 3 2 3
|
||||
|
||||
[1,2,3], [1,2,3]
|
||||
|
||||
Output: true
|
||||
```
|
||||
|
||||
Example 2:
|
||||
|
||||
```c
|
||||
Input: 1 1
|
||||
/ \
|
||||
2 2
|
||||
|
||||
[1,2], [1,null,2]
|
||||
|
||||
Output: false
|
||||
```
|
||||
|
||||
Example 3:
|
||||
|
||||
```c
|
||||
Input: 1 1
|
||||
/ \ / \
|
||||
2 1 1 2
|
||||
|
||||
[1,2,1], [1,1,2]
|
||||
|
||||
Output: false
|
||||
```
|
||||
|
||||
## 题目大意
|
||||
|
||||
这一题要求判断 2 颗树是否是完全相等的。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
递归判断即可。
|
||||
|
||||
|
Reference in New Issue
Block a user