-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSolution.kt
33 lines (28 loc) · 966 Bytes
/
Solution.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package _101.kotlin
import structure.TreeNode
/**
* @author relish
* @since 2018/04/26
*
* Definition for a binary tree node.
* class TreeNode(var `val`: Int = 0) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
class Solution {
private fun symmetric(left: TreeNode?, right: TreeNode?): Boolean {
if (left == null && right == null) return true
if (left == null || right == null) return false
return left.`val` == right.`val` && symmetric(left.right, right.left) && symmetric(left.left, right.right)
}
fun isSymmetric(root: TreeNode?): Boolean {
if (root == null) return true
if (root.left == null && root.right == null) return true
if (root.left == null || root.right == null) return false
return symmetric(root.left, root.right)
}
}
fun main(args: Array<String>) {
println(Solution().isSymmetric(TreeNode.createTestData("[1,2,2,null,3,null,3]")))
}