Skip to content

Commit a8f89ec

Browse files
committed
feat: add python and java solution to lcof problem
添加《剑指 Offer》题解:面试题28. 对称的二叉树
1 parent c1f56d8 commit a8f89ec

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# [面试题28. 对称的二叉树](https://leetcode-cn.com/problems/dui-cheng-de-er-cha-shu-lcof/)
2+
3+
## 题目描述
4+
请实现一个函数,用来判断一棵二叉树是不是对称的。如果一棵二叉树和它的镜像一样,那么它是对称的。
5+
6+
例如,二叉树 `[1,2,2,3,4,4,3]` 是对称的。
7+
8+
```
9+
    1
10+
   / \
11+
  2   2
12+
 / \ / \
13+
3  4 4  3
14+
```
15+
16+
但是下面这个 `[1,2,2,null,3,null,3]` 则不是镜像对称的:
17+
18+
```
19+
    1
20+
   / \
21+
  2   2
22+
   \   \
23+
   3    3
24+
```
25+
26+
**示例 1:**
27+
28+
```
29+
输入:root = [1,2,2,3,4,4,3]
30+
输出:true
31+
```
32+
33+
**示例 2:**
34+
35+
```
36+
输入:root = [1,2,2,null,3,null,3]
37+
输出:false
38+
```
39+
40+
**限制:**
41+
42+
- `0 <= 节点个数 <= 1000`
43+
44+
## 解法
45+
### Python3
46+
```python
47+
# Definition for a binary tree node.
48+
# class TreeNode:
49+
# def __init__(self, x):
50+
# self.val = x
51+
# self.left = None
52+
# self.right = None
53+
54+
class Solution:
55+
def isSymmetric(self, root: TreeNode) -> bool:
56+
if root is None:
57+
return True
58+
return self.symmetric(root.left, root.right)
59+
60+
def symmetric(self, node1, node2) -> bool:
61+
if node1 is None and node2 is None:
62+
return True
63+
if node1 is None or node2 is None or node1.val != node2.val:
64+
return False
65+
return self.symmetric(node1.left, node2.right) and self.symmetric(node1.right, node2.left)
66+
```
67+
68+
### Java
69+
```java
70+
/**
71+
* Definition for a binary tree node.
72+
* public class TreeNode {
73+
* int val;
74+
* TreeNode left;
75+
* TreeNode right;
76+
* TreeNode(int x) { val = x; }
77+
* }
78+
*/
79+
class Solution {
80+
public boolean isSymmetric(TreeNode root) {
81+
if (root == null) {
82+
return true;
83+
}
84+
return isSymmetric(root.left, root.right);
85+
}
86+
87+
private boolean isSymmetric(TreeNode node1, TreeNode node2) {
88+
if (node1 == null && node2 == null) {
89+
return true;
90+
}
91+
if (node1 == null || node2 == null || node1.val != node2.val) {
92+
return false;
93+
}
94+
return isSymmetric(node1.left, node2.right) && isSymmetric(node1.right, node2.left);
95+
}
96+
}
97+
```
98+
99+
### ...
100+
```
101+
102+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public boolean isSymmetric(TreeNode root) {
12+
if (root == null) {
13+
return true;
14+
}
15+
return isSymmetric(root.left, root.right);
16+
}
17+
18+
private boolean isSymmetric(TreeNode node1, TreeNode node2) {
19+
if (node1 == null && node2 == null) {
20+
return true;
21+
}
22+
if (node1 == null || node2 == null || node1.val != node2.val) {
23+
return false;
24+
}
25+
return isSymmetric(node1.left, node2.right) && isSymmetric(node1.right, node2.left);
26+
}
27+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def isSymmetric(self, root: TreeNode) -> bool:
10+
if root is None:
11+
return True
12+
return self.symmetric(root.left, root.right)
13+
14+
def symmetric(self, node1, node2) -> bool:
15+
if node1 is None and node2 is None:
16+
return True
17+
if node1 is None or node2 is None or node1.val != node2.val:
18+
return False
19+
return self.symmetric(node1.left, node2.right) and self.symmetric(node1.right, node2.left)

0 commit comments

Comments
 (0)