mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2026-03-13 10:02:05 +08:00
添加 problem 100
This commit is contained in:
22
Algorithms/100. Same Tree/100. Same Tree.go
Normal file
22
Algorithms/100. Same Tree/100. Same Tree.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package leetcode
|
||||
|
||||
/**
|
||||
* 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
|
||||
}
|
||||
}
|
||||
71
Algorithms/100. Same Tree/100. Same Tree_test.go
Normal file
71
Algorithms/100. Same Tree/100. Same Tree_test.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
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, 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 := Ints2TreeNode(p.one)
|
||||
rootTwo := Ints2TreeNode(p.two)
|
||||
fmt.Printf("【output】:%v \n", isSameTree(rootOne, rootTwo))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
51
Algorithms/100. Same Tree/README.md
Normal file
51
Algorithms/100. Same Tree/README.md
Normal file
@@ -0,0 +1,51 @@
|
||||
# [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