File tree 1 file changed +77
-0
lines changed
1 file changed +77
-0
lines changed Original file line number Diff line number Diff line change @@ -98,7 +98,84 @@ return True
98
98
> 其实更像本题一点的话应该是从中间分别向两边扩展 😂
99
99
100
100
## 代码
101
+ 代码支持:C++, Java, Python3
102
+
103
+ C++ Code:
104
+ ``` c++
105
+ /* *
106
+ * Definition for a binary tree node.
107
+ * struct TreeNode {
108
+ * int val;
109
+ * TreeNode *left;
110
+ * TreeNode *right;
111
+ * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
112
+ * };
113
+ */
114
+ class Solution {
115
+ public:
116
+ bool isSymmetric(TreeNode* root) {
117
+ return root==NULL?true: recur (root->left, root->right);
118
+ }
119
+
120
+ bool recur(TreeNode* l, TreeNode* r)
121
+ {
122
+ if(l == NULL && r==NULL)
123
+ {
124
+ return true;
125
+ }
126
+ // 只存在一个子节点 或者左右不相等
127
+ if (l==NULL || r==NULL || l->val != r->val)
128
+ {
129
+ return false;
130
+ }
131
+
132
+ return recur(l->left, r->right) && recur(l->right, r->left);
133
+ }
134
+ };
135
+ ```
136
+
137
+
138
+ Java Code:
139
+ ``` java
140
+ /**
141
+ * Definition for a binary tree node.
142
+ * public class TreeNode {
143
+ * int val;
144
+ * TreeNode left;
145
+ * TreeNode right;
146
+ * TreeNode(int x) { val = x; }
147
+ * }
148
+ */
149
+ class Solution {
150
+ public boolean isSymmetric (TreeNode root ) {
151
+ if (root == null )
152
+ {
153
+ return true ;
154
+ }
155
+ else {
156
+ return recur(root. left, root. right);
157
+ }
158
+ // return root == null ? true : recur(root.left, root.right);
159
+ }
160
+
161
+ public boolean recur (TreeNode l , TreeNode r )
162
+ {
163
+ if (l == null && r== null )
164
+ {
165
+ return true ;
166
+ }
167
+ // 只存在一个子节点 或者左右不相等
168
+ if (l== null || r== null || l. val != r. val)
169
+ {
170
+ return false ;
171
+ }
172
+
173
+ return recur(l. left, r. right) && recur(l. right, r. left);
174
+ }
175
+ }
176
+ ```
101
177
178
+ Python3 Code:
102
179
``` py
103
180
104
181
class Solution :
You can’t perform that action at this time.
0 commit comments