mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
规范格式
This commit is contained in:
47
leetcode/0101.Symmetric-Tree/101. Symmetric Tree.go
Normal file
47
leetcode/0101.Symmetric-Tree/101. Symmetric Tree.go
Normal file
@ -0,0 +1,47 @@
|
||||
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 isSymmetric(root *TreeNode) bool {
|
||||
if root == nil {
|
||||
return true
|
||||
}
|
||||
return isSameTree(invertTree(root.Left), root.Right)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
func invertTree(root *TreeNode) *TreeNode {
|
||||
if root == nil {
|
||||
return nil
|
||||
}
|
||||
invertTree(root.Left)
|
||||
invertTree(root.Right)
|
||||
root.Left, root.Right = root.Right, root.Left
|
||||
return root
|
||||
}
|
76
leetcode/0101.Symmetric-Tree/101. Symmetric Tree_test.go
Normal file
76
leetcode/0101.Symmetric-Tree/101. Symmetric Tree_test.go
Normal file
@ -0,0 +1,76 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/halfrost/LeetCode-Go/structures"
|
||||
)
|
||||
|
||||
type question101 struct {
|
||||
para101
|
||||
ans101
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para101 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans101 struct {
|
||||
one bool
|
||||
}
|
||||
|
||||
func Test_Problem101(t *testing.T) {
|
||||
|
||||
qs := []question101{
|
||||
|
||||
question101{
|
||||
para101{[]int{3, 4, 4, 5, structures.NULL, structures.NULL, 5, 6, structures.NULL, structures.NULL, 6}},
|
||||
ans101{true},
|
||||
},
|
||||
|
||||
question101{
|
||||
para101{[]int{1, 2, 2, structures.NULL, 3, 3}},
|
||||
ans101{true},
|
||||
},
|
||||
|
||||
question101{
|
||||
para101{[]int{}},
|
||||
ans101{true},
|
||||
},
|
||||
|
||||
question101{
|
||||
para101{[]int{1}},
|
||||
ans101{true},
|
||||
},
|
||||
|
||||
question101{
|
||||
para101{[]int{1, 2, 3}},
|
||||
ans101{false},
|
||||
},
|
||||
|
||||
question101{
|
||||
para101{[]int{1, 2, 2, 3, 4, 4, 3}},
|
||||
ans101{true},
|
||||
},
|
||||
|
||||
question101{
|
||||
para101{[]int{1, 2, 2, structures.NULL, 3, structures.NULL, 3}},
|
||||
ans101{false},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 101------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans101, q.para101
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
rootOne := structures.Ints2TreeNode(p.one)
|
||||
fmt.Printf("【output】:%v \n", isSymmetric(rootOne))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
43
leetcode/0101.Symmetric-Tree/README.md
Normal file
43
leetcode/0101.Symmetric-Tree/README.md
Normal file
@ -0,0 +1,43 @@
|
||||
# [101. Symmetric Tree](https://leetcode.com/problems/symmetric-tree/)
|
||||
|
||||
## 题目
|
||||
|
||||
|
||||
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
|
||||
|
||||
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
|
||||
|
||||
|
||||
```c
|
||||
1
|
||||
/ \
|
||||
2 2
|
||||
/ \ / \
|
||||
3 4 4 3
|
||||
```
|
||||
|
||||
But the following [1,2,2,null,3,null,3] is not:
|
||||
|
||||
```c
|
||||
1
|
||||
/ \
|
||||
2 2
|
||||
\ \
|
||||
3 3
|
||||
```
|
||||
|
||||
Note:
|
||||
|
||||
Bonus points if you could solve it both recursively and iteratively.
|
||||
|
||||
## 题目大意
|
||||
|
||||
这一题要求判断 2 颗树是否是左右对称的。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 这道题是几道题的综合题。将根节点的左字数反转二叉树,然后再和根节点的右节点进行比较,是否完全相等。
|
||||
- 反转二叉树是第 226 题。判断 2 颗树是否完全相等是第 100 题。
|
||||
|
||||
|
Reference in New Issue
Block a user