Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dc3cc0a

Browse files
🐱(tree): 101. 对称二叉树 补充 golang
1 parent 2775eba commit dc3cc0a

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

‎docs/data-structure/tree/other/README.md‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[原题链接](https://leetcode-cn.com/problems/symmetric-tree/description/)
44

5-
### 思路
5+
### 解一:递归
66

77
递归法,判断二叉树是否为镜像对称。
88

@@ -34,7 +34,9 @@ else:
3434
- A节点的左节点与B节点的右节点
3535
- A节点的右节点与B节点的左节点
3636

37-
### python 实现
37+
<!-- tabs:start -->
38+
39+
#### **Python**
3840

3941
```python
4042
# Definition for a binary tree node.
@@ -63,6 +65,37 @@ class Solution:
6365
return self.checkElement(left_root.left, right_root.right) and self.checkElement(left_root.right, right_root.left)
6466
```
6567

68+
#### **Go**
69+
70+
```go
71+
/**
72+
* Definition for a binary tree node.
73+
* type TreeNode struct {
74+
* Val int
75+
* Left *TreeNode
76+
* Right *TreeNode
77+
* }
78+
*/
79+
func isSymmetric(root *TreeNode) bool {
80+
if root == nil {
81+
return true
82+
}
83+
return symmetric(root.Left, root.Right)
84+
}
85+
86+
func symmetric(left *TreeNode, right *TreeNode) bool {
87+
if left == nil || right == nil {
88+
return left == right
89+
}
90+
if left.Val != right.Val {
91+
return false
92+
}
93+
return symmetric(left.Left, right.Right) && symmetric(left.Right, right.Left)
94+
}
95+
```
96+
97+
<!-- tabs:end -->
98+
6699
## 687. 最长同值路径
67100

68101
[原题链接](https://leetcode-cn.com/problems/longest-univalue-path/description/)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /