@@ -58,13 +58,13 @@ tags:
5858
5959### 方法一:递归
6060
61- 我们设计一个函数 $dfs( root1, root2)$,用于判断两个二叉树是否对称。答案即为 $dfs( root, root)$。
61+ 我们设计一个函数 $\textit{ dfs}(\textit{ root1}, \textit{ root2} )$,用于判断两个二叉树是否对称。答案即为 $\textit{ dfs}(\textit{ root.left}, \textit{ root.right} )$。
6262
63- 函数 $dfs( root1, root2)$ 的逻辑如下:
63+ 函数 $\textit{ dfs}(\textit{ root1}, \textit{ root2} )$ 的逻辑如下:
6464
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} $ 的左子树是否对称,这里使用了递归。
6868
6969时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点数。
7070
@@ -81,14 +81,14 @@ tags:
8181# self.right = right
8282class Solution :
8383 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:
8686 return True
8787 if root1 is None or root2 is None or root1.val != root2.val:
8888 return False
8989 return dfs(root1.left, root2.right) and dfs(root1.right, root2.left)
9090
91- return dfs(root, root)
91+ return dfs(root.left , root.right )
9292```
9393
9494#### Java
@@ -111,11 +111,11 @@ class Solution:
111111 */
112112class Solution {
113113 public boolean isSymmetric (TreeNode root ) {
114- return dfs(root, root);
114+ return dfs(root. left , root. right );
115115 }
116116
117117 private boolean dfs (TreeNode root1 , TreeNode root2 ) {
118- if (root1 == null && root2 == null ) {
118+ if (root1 == root2) {
119119 return true ;
120120 }
121121 if (root1 == null || root2 == null || root1. val != root2. val) {
@@ -143,12 +143,16 @@ class Solution {
143143class Solution {
144144public:
145145 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+ }
149153 return dfs(root1->left, root2->right) && dfs(root1->right, root2->left);
150154 };
151- return dfs(root, root);
155+ return dfs(root->left , root->right );
152156 }
153157};
154158```
@@ -165,17 +169,17 @@ public:
165169 * }
166170 */
167171func isSymmetric(root *TreeNode) bool {
168- var dfs func(*TreeNode, *TreeNode) bool
172+ var dfs func(root1, root2 *TreeNode) bool
169173 dfs = func(root1, root2 *TreeNode) bool {
170- if root1 == nil && root2 == nil {
174+ if root1 == root2 {
171175 return true
172176 }
173177 if root1 == nil || root2 == nil || root1.Val != root2.Val {
174178 return false
175179 }
176180 return dfs(root1.Left, root2.Right) && dfs(root1.Right, root2.Left)
177181 }
178- return dfs(root, root)
182+ return dfs(root.Left , root.Right )
179183}
180184```
181185
@@ -196,17 +200,16 @@ func isSymmetric(root *TreeNode) bool {
196200 * }
197201 */
198202
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-
209203function 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+ };
210213 return dfs (root .left , root .right );
211214}
212215```
@@ -235,23 +238,25 @@ function isSymmetric(root: TreeNode | null): boolean {
235238use std :: cell :: RefCell ;
236239use std :: rc :: Rc ;
237240impl 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+ }
244254 }
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- }
251255
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+ }
255260 }
256261}
257262```
@@ -272,79 +277,21 @@ impl Solution {
272277 * @return {boolean}
273278 */
274279var 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+ }
278287 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 );
281290};
282291```
283292
284293<!-- tabs: end -->
285294
286295<!-- solution: end -->
287296
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-
350297<!-- problem: end -->
0 commit comments