58
58
59
59
class Solution :
60
60
def isSymmetric (self , root : TreeNode) -> bool :
61
+ def is_symmetric (left , right ):
62
+ if left is None and right is None :
63
+ return True
64
+ if left is None or right is None or left.val != right.val:
65
+ return False
66
+ return is_symmetric(left.left, right.right) and is_symmetric(left.right, right.left)
61
67
if root is None :
62
68
return True
63
- return self .symmetric (root.left, root.right)
69
+ return is_symmetric (root.left, root.right)
64
70
65
- def symmetric (self , node1 , node2 ) -> bool :
66
- if node1 is None and node2 is None :
67
- return True
68
- if node1 is None or node2 is None or node1.val != node2.val:
69
- return False
70
- return self .symmetric(node1.left, node2.right) and self .symmetric(node1.right, node2.left)
71
71
```
72
72
73
73
### ** Java**
@@ -84,20 +84,14 @@ class Solution:
84
84
*/
85
85
class Solution {
86
86
public boolean isSymmetric (TreeNode root ) {
87
- if (root == null ) {
88
- return true ;
89
- }
87
+ if (root == null ) return true ;
90
88
return isSymmetric(root. left, root. right);
91
89
}
92
90
93
- private boolean isSymmetric (TreeNode node1 , TreeNode node2 ) {
94
- if (node1 == null && node2 == null ) {
95
- return true ;
96
- }
97
- if (node1 == null || node2 == null || node1. val != node2. val) {
98
- return false ;
99
- }
100
- return isSymmetric(node1. left, node2. right) && isSymmetric(node1. right, node2. left);
91
+ private boolean isSymmetric (TreeNode left , TreeNode right ) {
92
+ if (left == null && right == null ) return true ;
93
+ if (left == null || right == null || left. val != right. val) return false ;
94
+ return isSymmetric(left. left, right. right) && isSymmetric(left. right, right. left);
101
95
}
102
96
}
103
97
```
@@ -117,10 +111,10 @@ class Solution {
117
111
* @return {boolean}
118
112
*/
119
113
var isSymmetric = function (root ) {
120
- function dfs (a , b ) {
121
- if (! a && ! b ) return true ;
122
- if (! a || ! b ) return false ;
123
- return a . val === b . val && dfs (a .left , b .right ) && dfs (a .right , b .left );
114
+ function dfs (left , right ) {
115
+ if (! left && ! right ) return true ;
116
+ if (! left || ! right || left . val != right . val ) return false ;
117
+ return dfs (left .left , right .right ) && dfs (left .right , right .left );
124
118
}
125
119
if (! root) return true ;
126
120
return dfs (root .left , root .right );
@@ -130,24 +124,29 @@ var isSymmetric = function (root) {
130
124
### ** Go**
131
125
132
126
``` go
127
+ /* *
128
+ * Definition for a binary tree node.
129
+ * type TreeNode struct {
130
+ * Val int
131
+ * Left *TreeNode
132
+ * Right *TreeNode
133
+ * }
134
+ */
133
135
func isSymmetric (root *TreeNode ) bool {
134
136
if root == nil {
135
137
return true
136
138
}
137
139
return isSymme (root.Left , root.Right )
138
140
}
139
141
140
- func isSymme (a *TreeNode , b *TreeNode ) bool {
141
- if a == nil && b == nil {
142
+ func isSymme (left *TreeNode , right *TreeNode ) bool {
143
+ if left == nil && right == nil {
142
144
return true
143
145
}
144
- if a == nil || b ==nil {
145
- return false
146
- }
147
- if a.Val != b.Val {
146
+ if left == nil || right == nil || left.Val != right.Val {
148
147
return false
149
148
}
150
- return isSymme (a .Left ,b .Right ) && isSymme (a .Right , b .Left )
149
+ return isSymme (left .Left , right .Right ) && isSymme (left .Right , right .Left )
151
150
}
152
151
```
153
152
0 commit comments