@@ -58,13 +58,13 @@ tags:
58
58
59
59
### 方法一:递归
60
60
61
- 我们设计一个函数 $dfs( root1, root2)$,用于判断两个二叉树是否对称。答案即为 $dfs( root, root)$。
61
+ 我们设计一个函数 $\textit{ dfs}(\textit{ root1}, \textit{ root2} )$,用于判断两个二叉树是否对称。答案即为 $\textit{ dfs}(\textit{ root.left}, \textit{ root.right} )$。
62
62
63
- 函数 $dfs( root1, root2)$ 的逻辑如下:
63
+ 函数 $\textit{ dfs}(\textit{ root1}, \textit{ root2} )$ 的逻辑如下:
64
64
65
- - 如果 $root1$ 和 $root2$ 都为空,则两个二叉树对称,返回 ` true ` ;
66
- - 如果 $root1$ 和 $root2$ 中只有一个为空,或者 $root1.val \neq root2.val$,则两个二叉树不对称,返回 ` false ` ;
67
- - 否则,判断 $root1$ 的左子树和 $root2$ 的右子树是否对称,以及 $root1$ 的右子树和 $root2$ 的左子树是否对称,这里使用了递归。
65
+ - 如果 $\textit{ root1} $ 和 $\textit{ root2} $ 都为空,则两个二叉树对称,返回 ` true ` ;
66
+ - 如果 $\textit{ root1} $ 和 $\textit{ root2} $ 中只有一个为空,或者 $\textit{ root1.val} \neq \textit{ root2.val}$
67
+ - 否则,判断 $\textit{ root1} $ 的左子树和 $\textit{ root2} $ 的右子树是否对称,以及 $\textit{ root1} $ 的右子树和 $\textit{ root2} $ 的左子树是否对称,这里使用了递归。
68
68
69
69
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点数。
70
70
@@ -81,14 +81,14 @@ tags:
81
81
# self.right = right
82
82
class Solution :
83
83
def isSymmetric (self , root : Optional[TreeNode]) -> bool :
84
- def dfs (root1 , root2 ) :
85
- if root1 is None and root2 is None :
84
+ def dfs (root1 : Optional[TreeNode] , root2 : Optional[TreeNode]) -> bool :
85
+ if root1 == root2:
86
86
return True
87
87
if root1 is None or root2 is None or root1.val != root2.val:
88
88
return False
89
89
return dfs(root1.left, root2.right) and dfs(root1.right, root2.left)
90
90
91
- return dfs(root, root)
91
+ return dfs(root.left , root.right )
92
92
```
93
93
94
94
#### Java
@@ -111,11 +111,11 @@ class Solution:
111
111
*/
112
112
class Solution {
113
113
public boolean isSymmetric (TreeNode root ) {
114
- return dfs(root, root);
114
+ return dfs(root. left , root. right );
115
115
}
116
116
117
117
private boolean dfs (TreeNode root1 , TreeNode root2 ) {
118
- if (root1 == null && root2 == null ) {
118
+ if (root1 == root2) {
119
119
return true ;
120
120
}
121
121
if (root1 == null || root2 == null || root1. val != root2. val) {
@@ -143,12 +143,16 @@ class Solution {
143
143
class Solution {
144
144
public:
145
145
bool isSymmetric(TreeNode* root) {
146
- function<bool(TreeNode* , TreeNode* )> dfs = [ &] (TreeNode* root1, TreeNode* root2) -> bool {
147
- if (!root1 && !root2) return true;
148
- if (!root1 || !root2 || root1->val != root2->val) return false;
146
+ auto dfs = [ &] (this auto&& dfs, TreeNode* root1, TreeNode* root2) -> bool {
147
+ if (root1 == root2) {
148
+ return true;
149
+ }
150
+ if (!root1 || !root2 || root1->val != root2->val) {
151
+ return false;
152
+ }
149
153
return dfs(root1->left, root2->right) && dfs(root1->right, root2->left);
150
154
};
151
- return dfs(root, root);
155
+ return dfs(root->left , root->right );
152
156
}
153
157
};
154
158
```
@@ -165,17 +169,17 @@ public:
165
169
* }
166
170
*/
167
171
func isSymmetric(root *TreeNode) bool {
168
- var dfs func(*TreeNode, *TreeNode) bool
172
+ var dfs func(root1, root2 *TreeNode) bool
169
173
dfs = func(root1, root2 *TreeNode) bool {
170
- if root1 == nil && root2 == nil {
174
+ if root1 == root2 {
171
175
return true
172
176
}
173
177
if root1 == nil || root2 == nil || root1.Val != root2.Val {
174
178
return false
175
179
}
176
180
return dfs(root1.Left, root2.Right) && dfs(root1.Right, root2.Left)
177
181
}
178
- return dfs(root, root)
182
+ return dfs(root.Left , root.Right )
179
183
}
180
184
```
181
185
@@ -196,17 +200,16 @@ func isSymmetric(root *TreeNode) bool {
196
200
* }
197
201
*/
198
202
199
- const dfs = (root1 : TreeNode | null , root2 : TreeNode | null ) => {
200
- if (root1 == root2 ) {
201
- return true ;
202
- }
203
- if (root1 == null || root2 == null || root1 .val != root2 .val ) {
204
- return false ;
205
- }
206
- return dfs (root1 .left , root2 .right ) && dfs (root1 .right , root2 .left );
207
- };
208
-
209
203
function isSymmetric(root : TreeNode | null ): boolean {
204
+ const dfs = (root1 : TreeNode | null , root2 : TreeNode | null ): boolean => {
205
+ if (root1 === root2 ) {
206
+ return true ;
207
+ }
208
+ if (! root1 || ! root2 || root1 .val !== root2 .val ) {
209
+ return false ;
210
+ }
211
+ return dfs (root1 .left , root2 .right ) && dfs (root1 .right , root2 .left );
212
+ };
210
213
return dfs (root .left , root .right );
211
214
}
212
215
```
@@ -235,23 +238,25 @@ function isSymmetric(root: TreeNode | null): boolean {
235
238
use std :: cell :: RefCell ;
236
239
use std :: rc :: Rc ;
237
240
impl Solution {
238
- fn dfs (root1 : & Option <Rc <RefCell <TreeNode >>>, root2 : & Option <Rc <RefCell <TreeNode >>>) -> bool {
239
- if root1 . is_none () && root2 . is_none () {
240
- return true ;
241
- }
242
- if root1 . is_none () || root2 . is_none () {
243
- return false ;
241
+ pub fn is_symmetric (root : Option <Rc <RefCell <TreeNode >>>) -> bool {
242
+ fn dfs (root1 : Option <Rc <RefCell <TreeNode >>>, root2 : Option <Rc <RefCell <TreeNode >>>) -> bool {
243
+ match (root1 , root2 ) {
244
+ (Some (node1 ), Some (node2 )) => {
245
+ let node1 = node1 . borrow ();
246
+ let node2 = node2 . borrow ();
247
+ node1 . val == node2 . val
248
+ && dfs (node1 . left. clone (), node2 . right. clone ())
249
+ && dfs (node1 . right. clone (), node2 . left. clone ())
250
+ }
251
+ (None , None ) => true ,
252
+ _ => false ,
253
+ }
244
254
}
245
- let node1 = root1 . as_ref (). unwrap (). borrow ();
246
- let node2 = root2 . as_ref (). unwrap (). borrow ();
247
- node1 . val == node2 . val
248
- && Self :: dfs (& node1 . left, & node2 . right)
249
- && Self :: dfs (& node1 . right, & node2 . left)
250
- }
251
255
252
- pub fn is_symmetric (root : Option <Rc <RefCell <TreeNode >>>) -> bool {
253
- let node = root . as_ref (). unwrap (). borrow ();
254
- Self :: dfs (& node . left, & node . right)
256
+ match root {
257
+ Some (root ) => dfs (root . borrow (). left. clone (), root . borrow (). right. clone ()),
258
+ None => true ,
259
+ }
255
260
}
256
261
}
257
262
```
@@ -272,79 +277,21 @@ impl Solution {
272
277
* @return {boolean}
273
278
*/
274
279
var isSymmetric = function (root ) {
275
- function dfs (root1 , root2 ) {
276
- if (! root1 && ! root2) return true ;
277
- if (! root1 || ! root2 || root1 .val != root2 .val ) return false ;
280
+ const dfs = (root1 , root2 ) => {
281
+ if (root1 === root2) {
282
+ return true ;
283
+ }
284
+ if (! root1 || ! root2 || root1 .val !== root2 .val ) {
285
+ return false ;
286
+ }
278
287
return dfs (root1 .left , root2 .right ) && dfs (root1 .right , root2 .left );
279
- }
280
- return dfs (root, root);
288
+ };
289
+ return dfs (root . left , root . right );
281
290
};
282
291
```
283
292
284
293
<!-- tabs: end -->
285
294
286
295
<!-- solution: end -->
287
296
288
- <!-- solution: start -->
289
-
290
- ### 方法二
291
-
292
- <!-- tabs: start -->
293
-
294
- #### Rust
295
-
296
- ``` rust
297
- // Definition for a binary tree node.
298
- // #[derive(Debug, PartialEq, Eq)]
299
- // pub struct TreeNode {
300
- // pub val: i32,
301
- // pub left: Option<Rc<RefCell<TreeNode>>>,
302
- // pub right: Option<Rc<RefCell<TreeNode>>>,
303
- // }
304
- //
305
- // impl TreeNode {
306
- // #[inline]
307
- // pub fn new(val: i32) -> Self {
308
- // TreeNode {
309
- // val,
310
- // left: None,
311
- // right: None
312
- // }
313
- // }
314
- // }
315
- use std :: cell :: RefCell ;
316
- use std :: collections :: VecDeque ;
317
- use std :: rc :: Rc ;
318
- impl Solution {
319
- pub fn is_symmetric (root : Option <Rc <RefCell <TreeNode >>>) -> bool {
320
- let root = root . unwrap ();
321
- let mut node = root . as_ref (). borrow_mut ();
322
- let mut queue = VecDeque :: new ();
323
- queue . push_back ([node . left. take (), node . right. take ()]);
324
- while let Some ([root1 , root2 ]) = queue . pop_front () {
325
- if root1 . is_none () && root2 . is_none () {
326
- continue ;
327
- }
328
- if root1 . is_none () || root2 . is_none () {
329
- return false ;
330
- }
331
- if let (Some (node1 ), Some (node2 )) = (root1 , root2 ) {
332
- let mut node1 = node1 . as_ref (). borrow_mut ();
333
- let mut node2 = node2 . as_ref (). borrow_mut ();
334
- if node1 . val != node2 . val {
335
- return false ;
336
- }
337
- queue . push_back ([node1 . left. take (), node2 . right. take ()]);
338
- queue . push_back ([node1 . right. take (), node2 . left. take ()]);
339
- }
340
- }
341
- true
342
- }
343
- }
344
- ```
345
-
346
- <!-- tabs: end -->
347
-
348
- <!-- solution: end -->
349
-
350
297
<!-- problem: end -->
0 commit comments