Skip to content

Commit 00d1a49

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0100
See https://leetcode-cn.com/problems/symmetric-tree/
1 parent b13d582 commit 00d1a49

File tree

6 files changed

+152
-8
lines changed

6 files changed

+152
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282

8383
### 二叉树
8484

85+
1. [对称二叉树](/solution/0100-0199/0101.Symmetric%20Tree/README.md)
8586
1. [二叉树的最大深度](/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/README.md)
8687
1. [二叉树的最近公共祖先](/solution/0200-0299/0235.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree/README.md)
8788
1. [二叉搜索树的最近公共祖先](/solution/0200-0299/0236.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/README.md)

README_EN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
8080

8181
### Binary Tree
8282

83+
1. [Symmetric Tree](/solution/0100-0199/0101.Symmetric%20Tree/README_EN.md)
8384
1. [Maximum Depth of Binary Tree](/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/README_EN.md)
8485
1. [Lowest Common Ancestor of a Binary Tree](/solution/0200-0299/0236.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/README_EN.md)
8586
1. [Lowest Common Ancestor of a Binary Search Tree](/solution/0200-0299/0235.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree/README_EN.md)

solution/0100-0199/0101.Symmetric Tree/README.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,72 @@
4040
<!-- 这里可写当前语言的特殊实现逻辑 -->
4141

4242
```python
43-
43+
# Definition for a binary tree node.
44+
# class TreeNode:
45+
# def __init__(self, x):
46+
# self.val = x
47+
# self.left = None
48+
# self.right = None
49+
50+
class Solution:
51+
def isSymmetric(self, root: TreeNode) -> bool:
52+
if root is None:
53+
return True
54+
return self.is_symmetric(root.left, root.right)
55+
56+
def is_symmetric(self, left: TreeNode, right: TreeNode) -> bool:
57+
if left is None and right is None:
58+
return True
59+
if left is None or right is None or left.val != right.val:
60+
return False
61+
return self.is_symmetric(left.left, right.right) and self.is_symmetric(left.right, right.left)
4462
```
4563

4664
### **Java**
4765

4866
<!-- 这里可写当前语言的特殊实现逻辑 -->
4967

5068
```java
69+
/**
70+
* Definition for a binary tree node.
71+
* public class TreeNode {
72+
* int val;
73+
* TreeNode left;
74+
* TreeNode right;
75+
* TreeNode(int x) { val = x; }
76+
* }
77+
*/
78+
class Solution {
79+
public boolean isSymmetric(TreeNode root) {
80+
if (root == null) return true;
81+
return isSymmetric(root.left, root.right);
82+
}
83+
84+
private boolean isSymmetric(TreeNode left, TreeNode right) {
85+
if (left == null && right == null) return true;
86+
if (left == null || right == null || left.val != right.val) return false;
87+
return isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
88+
}
89+
}
90+
```
5191

92+
### **C++**
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
bool isSymmetric(TreeNode* root) {
98+
if (!root) return true;
99+
return isSymmetric(root->left, root->right);
100+
}
101+
102+
private:
103+
bool isSymmetric(TreeNode* left, TreeNode* right) {
104+
if (!left && !right) return true;
105+
if (!left && right || left && !right || left->val != right->val) return false;
106+
return isSymmetric(left->left, right->right) && isSymmetric(left->right, right->left);
107+
}
108+
};
52109
```
53110
54111
### **...**

solution/0100-0199/0101.Symmetric Tree/README_EN.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,70 @@ Bonus points if you could solve it both recursively and iteratively.</p>
5353
### **Python3**
5454

5555
```python
56-
56+
# Definition for a binary tree node.
57+
# class TreeNode:
58+
# def __init__(self, x):
59+
# self.val = x
60+
# self.left = None
61+
# self.right = None
62+
63+
class Solution:
64+
def isSymmetric(self, root: TreeNode) -> bool:
65+
if root is None:
66+
return True
67+
return self.is_symmetric(root.left, root.right)
68+
69+
def is_symmetric(self, left: TreeNode, right: TreeNode) -> bool:
70+
if left is None and right is None:
71+
return True
72+
if left is None or right is None or left.val != right.val:
73+
return False
74+
return self.is_symmetric(left.left, right.right) and self.is_symmetric(left.right, right.left)
5775
```
5876

5977
### **Java**
6078

6179
```java
80+
/**
81+
* Definition for a binary tree node.
82+
* public class TreeNode {
83+
* int val;
84+
* TreeNode left;
85+
* TreeNode right;
86+
* TreeNode(int x) { val = x; }
87+
* }
88+
*/
89+
class Solution {
90+
public boolean isSymmetric(TreeNode root) {
91+
if (root == null) return true;
92+
return isSymmetric(root.left, root.right);
93+
}
94+
95+
private boolean isSymmetric(TreeNode left, TreeNode right) {
96+
if (left == null && right == null) return true;
97+
if (left == null || right == null || left.val != right.val) return false;
98+
return isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
99+
}
100+
}
101+
```
62102

103+
### **C++**
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
bool isSymmetric(TreeNode* root) {
109+
if (!root) return true;
110+
return isSymmetric(root->left, root->right);
111+
}
112+
113+
private:
114+
bool isSymmetric(TreeNode* left, TreeNode* right) {
115+
if (!left && !right) return true;
116+
if (!left && right || left && !right || left->val != right->val) return false;
117+
return isSymmetric(left->left, right->right) && isSymmetric(left->right, right->left);
118+
}
119+
};
63120
```
64121
65122
### **...**
Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
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+
*/
110
class Solution {
211
public boolean isSymmetric(TreeNode root) {
3-
if(root == null) return true;
12+
if (root == null) return true;
413
return isSymmetric(root.left, root.right);
514
}
6-
public boolean isSymmetric(TreeNode leftNode, TreeNode rightNode) {
7-
if(leftNode == null && rightNode == null) return true;
8-
if(leftNode == null || rightNode == null) return false;
9-
return leftNode.val == rightNode.val && isSymmetric(leftNode.left, rightNode.right) &&
10-
isSymmetric(leftNode.right, rightNode.left);
15+
16+
private boolean isSymmetric(TreeNode left, TreeNode right) {
17+
if (left == null && right == null) return true;
18+
if (left == null || right == null || left.val != right.val) return false;
19+
return isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
1120
}
1221
}
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.is_symmetric(root.left, root.right)
13+
14+
def is_symmetric(self, left: TreeNode, right: TreeNode) -> bool:
15+
if left is None and right is None:
16+
return True
17+
if left is None or right is None or left.val != right.val:
18+
return False
19+
return self.is_symmetric(left.left, right.right) and self.is_symmetric(left.right, right.left)

0 commit comments

Comments
 (0)